6

I'm trying to get an access token, using the latest version of the google-api-php-client (take a look at this https://github.com/google/google-api-php-client), and I'm following this official doc: https://developers.google.com/api-client-library/php/guide/aaa_oauth2_web

The only difference is that I'd like to use the service Google_Service_Plus, instead of Google_Service_Urlshortener.

Basically I do these steps:

  • I initialize the Google Client $client specifying client ID, client secret, developer key, scopes and redirect URI (which points to my localhost Ubuntu 12.04 - PHP 5.3.10).
  • I initialize the Google Plus Service $plus using the Google Client $client.
  • I redirect the user to the Auth URL using $client->createAuthUrl().
  • The user can see the Consent Page and accepts.
  • I obtain an authorization code $_GET['code'] to use for token exchange.
  • Then, in the callback page, I try to do $client->authenticate($_GET['code']); but google server returns HTTP 400 and I get a Google_Auth_Exception: Error fetching OAuth2 access token, message: 'invalid_request'.

The flow breaks here.
What's wrong?

Any help will be appreciated.

Thanks in advance,
Niccolò.

2 Answers 2

8

I created an oauth2callback.php file and was experiencing this problem. The problem went away when I setup my Google_Client properly. At first I was only setting the clientId and secret. As soon as I set the redirectUri, my code started working. I didn't think I needed to set the redirectUri since I know the redirect had already occurred (I was on the redirect page). I figured this answer might help others experiencing a similar problem.

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

2 Comments

This was exactly my problem. Since I am using server-side-flow, the redirectUri had to be set to "postmessage". Then, voila!
What does postmessage mean? @CJThompson
4

Once I set the JSON configuration file for the $client, it worked for me.

Example:$client->setAuthConfigFile(__dir__.'/../conf/client_secrets.json');

1 Comment

In my case, I forgot to set client secret.

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.