5

I'm trying to setup the config.php with my values but I get this fatal error:

Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'.

I entered the client ID, secret, simple API key, location and sqlite database as the documentation says here: https://developers.google.com/glass/quickstart/php

Any idea of what I'm doing wrong?

Thanks in advance!

mimming
  • 13,974
  • 3
  • 45
  • 74
user2328837
  • 51
  • 1
  • 2

3 Answers3

2

This happens when the URL where you're hosting your Glassware is not listed as a valid OAuth 2.0 redirect URI on the API console. Follow these steps to resolve it.

  1. First, determine your OAuth 2.0 redirect URI. This is where traffic goes at the end of the OAuth dance. The correct value depends on where you're running the php quick start. If you're running it on a local web server, it's likely to be http://localhost/mirror-quickstart-php/oauth2callback.php. If you're running it on example.com, it might be https://example.com/oauth2callback.php.
  2. Go to the API console. This link will lead you to the right panel (services), but double check that you're on the correct project.
  3. Scrolling down you should see a section of the page that looks like this: enter image description here. Click on 'Edit Settings...'.
  4. This triggers a pop-up where you can enter your OAuth redirect URIs. Add your redirect URI to the top text area. Make sure to put each URI on its own line.enter image description here
  5. Reload the page for the PHP Quick Start and everything should work.

If you're still having an issue, please share the full error message on the page and the value you have for the base url in config.php.

mimming
  • 13,974
  • 3
  • 45
  • 74
  • 2
    Wouldn't the error message in this case be redirect_uri_mismatch? invalid_grant sounds like an error during the "code exchange step" after the callback, even though I'm not sure how that can happen... – Scarygami Apr 28 '13 at 21:25
  • Ahh good point. @user2328837, if the answer does not solve your issue, make sure that you are following the OAuth flow all the way through on the same account. Reuse of a code leading into the redirect, or a mismatched client ID at time of server side exchange are also likely causes. – mimming Apr 30 '13 at 00:06
  • Hi Jenny, in advance thanks for your help! I'm testing this in the subdomain http://glass.misinspiraciones.com/. – user2328837 May 02 '13 at 20:07
  • I followed your steps (same steps in the PHP guide) and now I'm facing other issue. In the first textarea (Authorized Redirect URIs) I added: http://glass.misinspiraciones.com/oauth2callback.php And in the second one (Authorized JavaScript Origins) I added: https://mirror-api-playground.appspot.com And in config.php the $base_url = "http://glass.misinspiraciones.com". – user2328837 May 02 '13 at 20:19
  • Hi Jenny, thanks for paying attention to me :) I changed to my localhost because that domain don´t have https. And now in my localhost host is saying **bold**"An error ocurred: Error calling POST https://www.googleapis.com/mirror/v1/timeline?key=................: (403) Access Not Configured"**bold**. It seems like my api simple key is wrong, but is what I have in my console... :( – user2328837 May 11 '13 at 12:41
  • Access Not Configured seems to indicate that the "Google Mirror API" is not turned to 'on' on the services panel of the API console. This might have happened if you changed your project ID to a new one and forgot to toggle the API on. – mimming May 12 '13 at 20:21
0

If you're getting the invalid_grant error from Google : Instead of using the client ID from google developer console as your value for client ID in the code, try using the email address value from developer console. This is actually what they expect. So, to confirm - the value for Client ID should be the value from developer console ending @developer.gserviceaccount.com

scgough
  • 5,099
  • 3
  • 30
  • 48
0

Had similar problem in Windows PS dotnet environment when trying to connect to DialogFlow Google Cloud Project recently and it appeared to be intermittent fault but it turned out to be global $env:GOOGLE_APPLICATION_CREDENTIALS in windows PowerShell was pointing to a key file set when first setup access to GCloud days before and since regenereated keys in GCP Console

After generating new key file and setting it with new generated key file using $env:GOOGLE_APPLICATION_CREDENTIALS="[Path_To_JSON_Key_File]" the problem went away but fix only lasted as long as the window was open, then after a while ran $env:GOOGLE_APPLICATION_CREDENTIALS on a newly opened PS window and noticed it was still pointing to the old key file!

setting the system environment variable GOOGLE_APPLICATION_CREDENTIALS to the value of [Path_To_JSON_New_Key_File] fixed it, all newly opened PS windows had the correct key file and authenticated correctly using Service Account and .Net Google Cloud API calls worked after that

pst
  • 56
  • 4