5

i'm using google-api-php-client to request an access token from the client 0auth flow, but it returns the error "Could not determine client ID from request".

My code is below.

My client_secret.json:

 "web": { "client_id":"123", "project_id":"1234", "auth_uri":"https://accounts.google.com/o/oauth2/auth", "token_uri":"https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs", "client_secret":"123456789", "redirect_uris":[ "http://localhost:7777/v1/oauth2callback" ], "javascript_origins":[ "http://localhost:7777" ] } } 

My auth URL creation:

 $client->setAuthConfig(base_path() . '/client_secret.json'); $client->addScope(\Google_Service_Calendar::CALENDAR); $client->setRedirectUri(URL::to('/') . '/v1/oauth2callback'); $client->setAccessType('offline'); $client->setPrompt('consent'); $client->setIncludeGrantedScopes(true); $url = $client->createAuthUrl(); print filter_var($url, FILTER_SANITIZE_URL); 

I'm then manually going to the URL:

https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=123-blah&redirect_uri=http%3A%2F%2Flocalhost%3A7777%2Fv1%2Foauth2callback&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&include_granted_scopes=true&prompt=consent

The auth flow all seems to work and returns me back to my url where I try to decode the token:

 $code = $request->code; $client = new Google\Client(); $accessToken = $client->fetchAccessTokenWithAuthCode($code); var_dump($accessToken); exit; 

Which gives me this error:

array(2) { ["error"]=> string(15) "invalid_request" ["error_description"]=> string(43) "Could not determine client ID from request." }

4
  • check the path to your client_secret.json it cant find it. Commented May 20, 2021 at 6:45
  • Nah that's all good it can find it. I get the same error when I actually hardcode the values. Commented May 20, 2021 at 7:29
  • i would also consider checking the spelling of your file. Do you have client_secret.json.json maybe? it cant find the file you need to figure out why. Commented May 20, 2021 at 7:50
  • The file name is fine, it's not that as I've hard coded the values and still the same issue. Commented May 20, 2021 at 20:51

2 Answers 2

12

Found the issue. In case anyone else runs into this problem it was simply because I hadn't loaded the auth configs before trying to fetch the access token.

e.g. Add this line before calling fetchAccessTokenWithAuthCode(). $client->setAuthConfig(base_path() . '/client_secret.json');

The error was throwing me off and I assumed it was an issue with retrieving the auth token.

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

1 Comment

Just want to add to this: make sure that the client_secret is also set in the JSON file passed to setAuthConfig, not just the client_id. When creating new credentials on the Google Cloud console, the secret may not be in the downloaded JSON file (propagation delay?), which happened to me. Cue 4 hours of debugging to figure out why it wasn't working. If it's missing you will get this error even if the client_id is set because the Google API library will not send the client_id if the client_secret is missing (requires both).
0

Please check the "Application" type when creating the Oauth Client. In this case you might want to select application type as "Desktop". It worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.