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