omn​​iauth OAuthException & OAuth::未经授权

发布于 2024-12-18 07:42:54 字数 1638 浏览 6 评论 0原文

我已经安装了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-facebook' 'omniauth-twitter' gems 到 gemfile

有两个问题:

  1. 当我去 http://localhost:3000/auth/facebook 时我明白了 { “错误”: { "message": "缺少 client_id 参数。", “类型”:“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!!!

  1. 当我转到 http://localhost:3000/ auth/twitter 我收到 OAuth::Unauthorized

401 Unauthorized 有

什么想法吗?

I have installed omniauth 1.0. Also I have 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

Also I add
'omniauth'
'omniauth-facebook'
'omniauth-twitter' gems to gemfile

There are two problems:

  1. When I go http://localhost:3000/auth/facebook I get
    {
    "error": {
    "message": "Missing client_id parameter.",
    "type": "OAuthException"
    }
    }

And the link 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
And there is no client_id!!!

  1. When I go to http://localhost:3000/auth/twitter I get OAuth::Unauthorized

401 Unauthorized

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-12-25 07:42:54

Alex D. 是正确的,因为 ENV[] 打破了它。要创建omniauth.rb,以便它在不同的环境中使用不同的密钥,只需将:

provider :twitter, TWITTER_KEY, TWITTER_SECRET

放在omniauth.rb中

,然后在您的环境配置文件(config/environments/development.rb等)中放入您想要用于该环境的密钥。

配置/环境/开发.rb:

TWITTER_KEY = 'aaaaaaa'
TWITTER_SECRET = 'aaaabbbbbb'

配置/环境/生产.rb:

TWITTER_KEY = 'ccccccc'
TWITTER_SECRET = 'ccccdddddd'

Alex D. is correct in that the ENV[] breaks it. To create omniauth.rb so that it uses different keys in different environments just put:

provider :twitter, TWITTER_KEY, TWITTER_SECRET

in omniauth.rb

and then in your environment config files (config/environments/development.rb, etc.) put the key you want to use for that environment.

config/environments/development.rb:

TWITTER_KEY = 'aaaaaaa'
TWITTER_SECRET = 'aaaabbbbbb'

config/environments/production.rb:

TWITTER_KEY = 'ccccccc'
TWITTER_SECRET = 'ccccdddddd'
-黛色若梦 2024-12-25 07:42:54

ENV['something']

会查找您的环境变量中的“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='....'

ENV['something']

looks into your environment vars for "something", so it would expect

something='12345'

so you should do it like that

export AUTH_FB_KEY='....'
export AUTH_FB_SECRET='...'

check with

env

and update your config

provider :facebook, ENV['AUTH_FB_KEY'], ENV['AUTH_FB_SECRET']

if you use heroku

heroku config:add AUTH_FB_KEY='....'
猫卆 2024-12-25 07:42:54

omn​​iauth 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中:

gem omniauth, '~> 0.3.2'

There have been breaking changes made in omniauth 1.0 - https://github.com/intridea/omniauth

OmniAuth 1.0 has several breaking changes from version 0.x. You can
set the dependency to ~> 0.3.2 if you do not wish to make the more
difficult upgrade. See the wiki for more information.

I would try reverting omniauth to 0.3.2:

gem install omniauth --version '~> 0.3.2'

or if you're using bundler, in your Gemfile:

gem omniauth, '~> 0.3.2'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文