1

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

6
  • Just an idea, but omit the last / from the redirect_uri. Or escape that somehow. Commented Sep 12, 2014 at 12:20
  • Doesn't make any difference Commented Sep 12, 2014 at 12:37
  • What you said. I remove the trailing slash from the redirect_url and also edited the redirect_url to match in the console Commented Sep 12, 2014 at 12:49
  • Hmm, try swapping redirect_uri and grant_type. Commented Sep 12, 2014 at 12:54
  • Still doesn't work. I just took out the redirect_url and it didn't even complain about that, it just said grant_type missing Commented Sep 12, 2014 at 13:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.