Authentication problems when upgrading to SpeechClient v1

100 Views Asked by At

In the project we have been using the google/cloud package's SpeechClient. I'm now in the process of upgrading it to the not deprecated /V1/SpeechClient, but I'm having an issue where I get the following error

"Google\ApiCore\ApiException: {
    "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.",
    "code": 16,
    "status": "UNAUTHENTICATED",
    "details": []
}

This authentication info works in the previous version, and I've tried to change to the new ways it wants me to authenticate, but I guess I'm not doing it correctly. Any help?


Previous setup:

    // $auth is keyfile in an Array format. it matches the design given in here, with the edit fields filled
    // https://cloud.google.com/iam/docs/keys-create-delete#iam-service-account-keys-create-console
    $auth = [
         "type" => "service_account",
         "project_id" => "<project_id>",
         "private_key_id" => "<private_key_id>",
         "private_key" => "-----BEGIN PRIVATE KEY-----\n<key>\n-----END PRIVATE KEY-----",
         "client_email" => "<the gserviceaccount.com>",
         "client_id" => "<client_id>",
         "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_x509_cert_url" => "https://www.googleapis.com/robot/v1/metadata/<our url>.gserviceaccount.com",
    ];
    $speech = new Google\Cloud\Speech\SpeechClient([
        'keyFile' => $auth,
    ]);


    // Same auth used for storageClient
    $storage = new Google\Cloud\Storage\StorageClient([
        'keyFile' => $auth
    ]);

Upgraded:

    // $auth is the same as above

    $creds = CredentialsWrapper::build([
        'keyFile' => $auth
    ]);

    // Create the speech client
    $speech = new Google\Cloud\Speech\V1\SpeechClient([
        'credentials' => $creds,
    ]);


    // old auth still used for Storage
    $storage = new Google\Cloud\Storage\StorageClient([
        'keyFile' => $auth
    ]);

    ...

    $operation = $speech->longRunningRecognize(
            $config,
            $audio
        );

    // gives me output as normal
    $operation->getName();

    // throws exception at the top of the post
    $operation->reload();

1

There are 1 best solutions below

1
Sushma Jeerahalli On

it could be that The Application Default Credentials are not available. Try to login by running

gcloud auth application-default login

And follow the instructions, reference: gcloud auth application-default login

If you want to make sure the authentication process went well,

run: gcloud auth application-default print-access-token

You should see an access token, reference gcloud auth application-default print-access-token