2

好的,已经超过 5 个小时了,我仍然无处可去。我想做的是在我的一个基于 Ruby-Sinatra 的应用程序中设置omniauth-gihub gem。以下是我所做的。

将 Gems 添加到 Gemfile(当然还有 Ran bundler update 命令):

source 'https://rubygems.org'

gem 'sinatra'
gem 'haml'
gem 'shotgun'
gem 'omniauth', :git => 'git://github.com/intridea/omniauth.git'
gem 'omniauth-github', :git => 'git://github.com/intridea/omniauth-github.git'

在我的 app.rb 文件下,我有以下代码:

#imports
require 'rubygems'
require 'bundler'
require 'sinatra'
require 'omniauth'
require 'omniauth-github'
require 'haml'
require './helpers.rb'

#Configure OmniAuth
use OmniAuth::Builder do
  provider :github, ENV['api_key'], ENV['secret'], # Removing the key and secret for security reasons
  scope: "user,repo,gist"
end

#Application Settings
set :sessions, true
set :views, 'templates'


#Get Method for Application Root
get '/' do
  haml :index
end

#Get/Post Methods For Authentication
%w(get post).each do |method|
  send(method, "/auth/:provider/callback") do
    env['omniauth.auth']
  end 
end

Github 应用的设置如下:

URL = http://127.0.0.1:4567
Callback URL = http://127.0.0.1:4567/auth/github/callback

现在每当我访问 127.0.0.1:4567/auth/github/callback 时,我都会收到以下错误:

I, [2012-07-26T07:05:23.540462 #30458]  INFO -- omniauth: (github) Callback phase initiated.
E, [2012-07-26T07:05:23.540700 #30458] ERROR -- omniauth: (github) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, OmniAuth::Strategies::OAuth2::CallbackError
localhost - - [26/Jul/2012:07:05:23 IST] "GET /auth/github/callback HTTP/1.1" 302 9
- -> /auth/github/callback
localhost - - [26/Jul/2012:07:05:23 IST] "GET /auth/failure?message=invalid_credentials&strategy=github HTTP/1.1" 404 448
- -> /auth/failure?message=invalid_credentials&strategy=github
localhost - - [26/Jul/2012:07:05:23 IST] "GET /favicon.ico HTTP/1.1" 404 447
- -> /favicon.ico

似乎它甚至没有尝试连接到 github,我以为我已经登录了所以我退出了 github 并尝试再次访问 127.0.0.4567/auth/github/callback 是的,它甚至没有连接或发送任何信息到 github .

我检查了我的 api 密钥和秘密,它们是正确的。我真的不知道我错过了什么,真的很累。任何帮助或建议将不胜感激。

编辑::

好的我发现在oauth2.rb中引发错误的代码如下

def callback_phase
    if request.params['error'] || request.params['error_reason']
      raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
    end 
    if request.params['state'].to_s.empty? || request.params['state'] != session.delete('omniauth.state')
      raise CallbackError.new(nil, :csrf_detected)
    end

我觉得这与 CSRF 有关。

4

2 回答 2

2

这可能很有趣: https ://github.com/intridea/omniauth-github/issues/12

我遇到了与您相同的错误,并添加了 scope: 'user' 为我修复了它。

我看到您已经在使用范围,但该链接可能会让您走上正确的轨道。

于 2012-07-27T05:22:33.297 回答
2

有同样的问题 - 将 omniauth-facebook 降级到 1.4.0 为我修复了它。https://github.com/mkdynamic/omniauth-facebook/issues/73

于 2012-08-06T16:44:00.940 回答