Reputation: 484
I'm getting an invalid client error. Client works for auth_code.authorize_url, but not for auth_code.get token
relevant 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)
Error message:
/.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>'
One clarification I think I need is that say the code given from the url is /?code=$code$
do I use just the $code$?
(currently doing this) or code=$code$
Thanks in advance for the help
Upvotes: 0
Views: 761
Reputation: 484
Fixed:
The answer was the allow offline mode when initializing the client. I was also using a code I had previously used.
changed this:
client = OAuth2::Client.new(CLIENT_ID, CLIENT_SECRET,
site: 'https://accounts.google.com',
token_url: '/o/oauth2/token',
authorize_url: '/o/oauth2/auth')
to this:
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
Upvotes: 1