Reputation: 3096
I have been trying for the last couple of days trying to get an access token and refresh token using Google Oauth2 but not getting anywhere.
The documentation is rubbish and I can't find any examples that work.
I have just started to use CURL trying to get the access code and refresh token but I keep getting errors with the response.
I have tried using this
// init the resource
$url = 'https://accounts.google.com/o/oauth2/token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-length: 0'
));
curl_setopt($ch, CURLOPT_POSTFIELDS,
'code=' . urlencode('AUTHORISATION_CODE') . '&' .
'client_id=' . urlencode('MY_CLINET_ID.apps.googleusercontent.com') . '&' .
'client_secret=' . urlencode('MY_CLIENT_SECRET') . '&' .
'redirect_uri=http://www.example.com/' . '&' .
'grant_type=authorization_code'
);
// execute
$output = curl_exec($ch);
echo $output;
// free
curl_close($ch);
but i just get this response
{
"error" : "invalid_request",
"error_description" : "Required parameter is missing: grant_type"
}
and I've tried running CURL from the command line
curl --data "code=AUTHORISATION_CODE&client_id=MY_CLIENT_ID.apps.googleusercontent.com&client_secret=MY_CLIENT_SECRET&redirect_uri=http://www.example.com/&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
but i just get a different response
{
"error" : "invalid_client",
"error_description" : "The OAuth client was not found."
}
Why is it so difficult just to get an access token?
UPDATE
I don't know what's happening but I don't receive that error anymore and I get an access token but not a refresh token
I've looked at this post Not receiving Google OAuth refresh token and I've revoked access to my app to regenerate the refresh token but using this code displays an error about an invalid code
Upvotes: 1
Views: 4968
Reputation: 3096
I have now fixed the problem using Curl. Didn't seem any reason why the above didn't work as just keeping trying, it worked eventually after revoking access to app, authing the app, getting the code and using the code in the Curl request.
This solved my previous question YouTube API v3 keeps asking for authorisation every time
Upvotes: 1