OAuth2 错误客户端无效

OAuth2 error invalid client

我收到无效客户端错误。客户端适用于 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>'

我想我需要澄清的是,url 给出的代码是 /?code=$code$ 我是只使用 $code$?(目前正在这样做)还是 code=$code$

在此先感谢您的帮助

固定:

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

改变了这个:

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