user2328837
user2328837

Reputation: 51

Google_AuthException : invalid_grant

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!

Upvotes: 5

Views: 7572

Answers (3)

pst
pst

Reputation: 56

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

Upvotes: 0

scgough
scgough

Reputation: 5252

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

Upvotes: 0

mimming
mimming

Reputation: 13954

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.

Upvotes: 2

Related Questions