omniauth OAuthException&OAuth ::未经授权
我已经安装了omniauth 1.0。 我也有oauth-0.4.5,oauth2-0.5.1,omniauth-facebook-1.0.0,omniauth-twitter-0.0.6。
omniauth.rb Rails.application.config.middleware.use OmniAuth::Builder do provider :developer unless Rails.env.production? provider :facebook, ENV['167257285348131'], ENV['c8c722f697scb2afcf1600286c6212a9'], :scope => 'email,offline_access,read_stream', :display => 'popup' provider :twitter, ENV['fma2L22ObJCW52QrL7uew'], ENV['4aZfhCAOdiS7ap8pHJ7I1OZslFwVWWLiAMVpYUI'] end session_controller.rb class SessionsController < ApplicationController require 'omniauth-facebook' require 'omniauth-twitter' require 'omniauth' def create @user = User.find_or_create_from_auth_hash(auth_hash) self.current_user = @user redirect_to '/' end def auth_hash request.env['omniauth.auth'] end end
此外,我将’omniauth”omniauth-facebook”omniauth-twitter’gem添加到gemfile中
有两个问题:
- 当我去http:// localhost:3000 / auth / facebook时,我得到{“error”:{“message”:“缺少client_id参数。”,“type”:“OAuthException”}}
链接graph.facebook.com/oauth/authorize?response_type=code&client_id=&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Ffacebook%2Fcallback&parse=query&scope=email%2Coffline_access%2Cread_stream&display=popup 并且没有client_id! !
- 当我去http:// localhost:3000 / auth / twitter时,我得到OAuth :: Unauthorized
401未经授权
有任何想法吗?
Alex D.是正确的,因为ENV []打破了它。 要创建omniauth.rb以便它在不同的环境中使用不同的密钥,只需:
provider :twitter, TWITTER_KEY, TWITTER_SECRET
在omniauth.rb
然后在您的环境中配置文件(config / environments / development.rb等)放置您要用于该环境的密钥。
配置/环境/ development.rb:
TWITTER_KEY = 'aaaaaaa' TWITTER_SECRET = 'aaaabbbbbb'
配置/环境/ production.rb:
TWITTER_KEY = 'ccccccc' TWITTER_SECRET = 'ccccdddddd'
ENV['something']
调查你的环境变量“东西”,所以它会期待
something='12345'
所以你应该那样做
export AUTH_FB_KEY='....' export AUTH_FB_SECRET='...'
检查
env
并更新您的配置
provider :facebook, ENV['AUTH_FB_KEY'], ENV['AUTH_FB_SECRET']
如果你使用heroku
heroku config:add AUTH_FB_KEY='....'
在omniauth 1.0中进行了重大更改 – https://github.com/intridea/omniauth
OmniAuth 1.0与版本0.x有几处重大更改。 如果您不希望升级更困难,可以将依赖关系设置为〜> 0.3.2。 有关更多信息,请参阅Wiki 。
我会尝试将omniauth恢复为0.3.2:
gem install omniauth --version '~> 0.3.2'
或者如果您在Gemfile中使用bundler:
gem omniauth, '~> 0.3.2'