Rails 3.1 OAuth提供程序“invalid_grant”与devise_oauth2_providable

问题描述:

我想设置一个oauth2提供程序已经用尽了omniauth和设计的应用程序。该user.rb现在是:Rails 3.1 OAuth提供程序“invalid_grant”与devise_oauth2_providable

class User < ActiveRecord::Base 

    has_many :authentications 
    has_many :graphs 

    devise :database_authenticatable, 
    :registerable, 
    :recoverable, 
    :rememberable, 
    :trackable, 
    :validatable, 
    :omniauthable, 
    :token_authenticatable, 
    :oauth2_providable, 
    :oauth2_password_grantable, 
    :oauth2_refresh_token_grantable, 
    :oauth2_authorization_code_grantable 

....... 

} 

一切是如预期实现,但我收到以下错误,当我运行这个所有本地和尝试使用的oauth2宝石(0.5.0)连接到我客户。

我结束了在:

http://localhost:8080/oauth/callback?code=2ebd3d9d149b22becec37da7a8f1eb0d

这引发异常:

e.to_yaml 
"--- !ruby/exception:OAuth2::Error\nresponse: &70266332040280 !ruby/object:OAuth2::Response\n response: &70266332040340 !ruby/object:Faraday::Response\n env:\n  :method: :post\n  :body: ! '{\"error\":\"invalid_grant\",\"error_description\":\"invalid authorization\n  code request\"}'\n  :url: !ruby/object:Addressable::URI\n  validation_deferred: false\n  scheme: http\n  normalized_scheme: !!null \n  uri_string: !!null \n  hash: !!null \n  host: localhost\n  authority: !!null \n  normalized_host: !!null \n  port: 3000\n  normalized_port: !!null \n  path: /oauth2/token\n  normalized_path: !!null \n  query: !!null \n  normalized_query: !!null \n  :request_headers:\n  Content-Type: application/x-www-form-urlencoded\n  :parallel_manager: !!null \n  :request:\n  :proxy: !!null \n  :ssl: {}\n  :status: 400\n  :response_headers:\n  content-type: application/json\n  x-ua-compatible: IE=Edge\n  cache-control: no-cache\n  x-runtime: '0.119701'\n  content-length: '82'\n  server: WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)\n  date: Wed, 05 Oct 2011 14:40:10 GMT\n  connection: close\n  :response: *70266332040340\n on_complete_callbacks: []\n options:\n :parse: !!null \n error: !ruby/exception:OAuth2::Error\n response: *70266332040280\n code: invalid_grant\n description: invalid authorization code request\n parsed:\n error: invalid_grant\n error_description: invalid authorization code request\ncode: invalid_grant\ndescription: invalid authorization code request\n" 

我也注意到,没有一个国家提供,这在最新的草案(HTTP://回调需要tools.ietf.org/html/draft-ietf-oauth-v2-22)。这可能是一个问题吗?

至于输出,我不是在服务器输出获得任何明显的错误:

http://pastie.org/2644028

现在,另一个值得注意的事情是,我看到一些选择是怎么回事,但我从来没有看到插入的任何数据到不同的表中,尤其是不会发生在抛出此异常之前发生的authorization_codes。

我不是Rails的天才,但我知道我的方式GitHub,我设法找到一些人提交他们的叉子承诺。通过一些有效的寻找修复合并到一个位置,我成功地创建一个叉细为我工作:

https://github.com/socialcast/devise_oauth2_providable/pull/17

我设置为我的Gemfile中源,现在回调正常工作!

现在,我只需要解决如何停止访问令牌到期,或者将user_id注入到darn响应中......工作工作。

查看查询后,它看起来像当前时间已超过expired_at值。下面是调用此错误devise_oauth2_providable代码:

module Devise 
    module Strategies 
    class Oauth2AuthorizationCodeGrantTypeStrategy < Oauth2GrantTypeStrategy 
     def grant_type 
     'authorization_code' 
     end 

     def authenticate! 
     if client && code = AuthorizationCode.valid.find_by_token(params[:code]) 
      success! code.user 
     elsif !halted? 
      oauth_error! :invalid_grant, 'invalid authorization code request' 
     end 
     end 
    end 
    end 
end 

这有效范围检查,以查看当前时间大于或等于AuthorizationCode的expires_at场。

+0

通常在询问所有人的帮助后,我再次解决了这个问题。我浏览了整个叉子网络并固定在github上,然后顺利完成! https://github.com/philsturgeon/devise_oauth2_providable你在这段代码中的改变是什么?如果它固定在我的叉子上,那么我会给你250点的时间来看看这个! –

+0

哦,我没有对代码做任何修改。我只注意到查询'code = AuthorizationCode.valid.find_by_token(params [:code])'返回nil,因为authorization_code已过期,导致调用invalid_grant错误。 –

+0

啊,我明白了。是的,我有这么多,没有令牌被发现,因为没有被插入到数据库中,正如我在帖子中提到的。我不再确定确切的解决方法,但是将多个有效的固定合并到我的叉中似乎完成了这项工作。谢谢! –