OAuth2错误无效的客户端

时间:2015-09-24 14:18:53

标签: ruby oauth-2.0 google-api

我收到了无效的客户端错误。客户端适用于auth_code.authorize_url,但不适用于auth_code.get令牌

相关代码:

CLIENT_ID = "$$$.apps.googleusercontent.com"
CLIENT_SECRET = "secret"

REDIRECT_URI = 'http://localhost:3000'
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, 
                                   site:     'https://accounts.google.com',
                                   token_url: '/o/oauth2/token',
                                   authorize_url: '/o/oauth2/auth')

url = client.auth_code.authorize_url(scope: "https://www.google.com/m8/feeds",
                                            redirect_uri: REDIRECT_URI)
puts url

code = "taken from url"  #line 20
token = client.auth_code.get_token(code, :redirect_uri => REDIRECT_URI)

错误讯息:

/.rvm/gems/ruby-2.2.1/gems/oauth2-1.0.0/lib/oauth2/client.rb:113:in `request': invalid_client:  (OAuth2::Error)
{
  "error" : "invalid_client"
}
from /.rvm/gems/ruby-2.2.1/gems/oauth2-1.0.0/lib/oauth2/client.rb:138:in `get_token'
from /.rvm/gems/ruby-2.2.1/gems/oauth2-1.0.0/lib/oauth2/strategy/auth_code.rb:29:in `get_token'
from oauth.rb:20:in `<main>'

我认为我需要的一个澄清是说,网址提供的代码是/?code=$code$我只使用$code$?(目前正在执行此操作)或code=$code$

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

修正:

答案是初始化客户端时允许脱机模式。我还使用了以前使用的代码。

改变了这个:

client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, 
                               site:     'https://accounts.google.com',
                               token_url: '/o/oauth2/token',
                               authorize_url: '/o/oauth2/auth')

到此:

client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET, 
                                   site: 'https://accounts.google.com',
                                   token_url: '/o/oauth2/token',
                                   authorize_url: '/o/oauth2/auth',
                                   additional_parameters: {"access_type" => "offline"} ) #new line