Google Cloud API php authentication fails

I closely followed the docs

https://github.com/googleapis/google-api-php-client/blob/main/docs/oauth-server.md

https://developers.google.com/android-publisher/getting_started?hl=en

  1. Created an service account on Google Cloud Console

  2. generated the key json file and saved as credentials.json

  3. enabled Google Play Android Developer API

  4. invited the service account email in the Google Play Console

  5. granted the permissions

  6. i use this code in php

     putenv('GOOGLE_APPLICATION_CREDENTIALS='.__DIR__.'/credentials.json');
     $client = new Google\Client();
     $client->useApplicationDefaultCredentials();
     $client->addScope('https://androidpublisher.googleapis.com');
     $service = new Google_Service_AndroidPublisher($client);
    

We get this error message:

"error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    /n"errors": [
      {
        "message": "Invalid Credentials",
        "domain": "global",
        "reason": "authError",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED"

May or may not be relevant, but the google account on Google Cloud and Google Play Console is the same and part of Google Workspaces but not the organisations account (the same procedure with the organisation account yields the same error message though)

  • Did you tried to echo GOOGLE_APPLICATION_CREDENTIALS to check if the path was correct ?

    – 

  • I changed the path once to test that and then received this error Unable to read the credential file specified by GOOGLE_APPLICATION_CREDENTIALS: file /credentials.json does not exist so i assume the path is correct, credentials.json is the unaltered keys file from the service account from google cloud

    – 




  • Sound good. Did you tried to regenerate a credentials.json file from GCP Console ?

    – 

  • of course – mutiple times, even created a fresh service account

    – 

  • The error invalid authentication credentials means that this is not working $client->useApplicationDefaultCredentials(); However, you do not show the code that requests credentials. The code you show only sets up data structures. When your code finally makes an API call, the Authorization header contains invalid data resulting in the error that you see.

    – 

Leave a Comment