Jesse Stay
Jesse Stay

Reputation: 73

refresh_token Invalid Credentials errors

I'm stumped. I'm trying to manually get a refresh token to load an access token for me on Mirror API using Perl and it keeps giving me credentials errors. When I load the exact HTTP request in the PHP example code (I've printed out the HTTP to compare) the same refresh_token works fine.

Here's my Perl HTTP request:

*POST https://accounts.google.com/o/oauth2/token Host: accounts.google.com User-Agent: libwww-perl/6.02 Content-Length: 175 Content-Type: application/x-www-form-urlencoded client_id=client_id_goes_here&client_secret=client_secret_goes_here&refresh_token=refresh_token_goes_here&grant_type=refresh_token*

Here's the PHP on the same refresh_token:

*POST /o/oauth2/token HTTP/1.1 content-type: application/x-www-form-urlencoded content-length: 175 client_id=client_id_goes_here&client_secret=client_secret_goes_here&refresh_token=refresh_token_goes_here&grant_type=refresh_token*

My Perl looks like this:

my $auth_response = $ua->request(POST 'https://accounts.google.com/o/oauth2/token',
            'Host'          => 'accounts.google.com',
            'Content_Type'  => 'application/x-www-form-urlencoded',
            'Content'       => [
                'client_id'         =>  $client_id,
                'client_secret'     =>  $client_secret,
                'refresh_token'     =>  $credentials->{'refresh_token'},
                'grant_type'        =>  'refresh_token',
            ],
        );

HELP! :-)

Upvotes: 1

Views: 376

Answers (1)

mimming
mimming

Reputation: 13954

It looks like you're using LWP. I hacked up this quick example which uses LWP to dance the OAuth 2.0 dance with Google from start to token refresh.

Based on my experimentation, the code you've shown so far looks correct. Here's the exact code I used to refresh my access token:

my $auth_response = $ua->request(POST 'https://accounts.google.com/o/oauth2/token',
            'Host'          => 'accounts.google.com',
            'Content_Type'  => 'application/x-www-form-urlencoded',
            'Content'       => [
                'client_id'         =>  $client_id,
                'client_secret'     =>  $client_secret,
                'refresh_token'     =>  $refresh_token,
                'grant_type'        =>  'refresh_token',
            ],
        );

If you're still observing an error, try cloning that repo, populating your client_id and client_secret, and seeing if the problem persists. If it does, please share the result of print Dumper($auth_response); which will provide a lot of useful info.

Also, Perl isn't a language officially supported by Google, but it looks like the community has come through: there's an open source Perl client library. I've never used it before, but you may want to check it out.

Upvotes: 1

Related Questions