我得到一个无效的客户端错误。客户端为auth_code工作。Authorize_url,而不是auth_code。获得令牌
相关代码: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