justin Yang
justin Yang

Reputation: 57

Gcloud error 401: REquest had invalid authentication credentials

I'm working on a project where I need to access a Gcloud service account. However, I've been encountering issues with authentication. This is the following error from my command prompt:

My command:

curl -s -H "Content-Type: application/json" \ 
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ 
https://speech.googleapis.com/v1/speech:recognize \ 
-d @sync-request.json 

Output:

{
  "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.",
    "status": "UNAUTHENTICATED"
  }
}

At the current moment, I have done the following: I set my environment variable, "GOOGLE_APPLICATION_CREDENTIALS", to hold the path to my service account's JSON key file, I opened my cmd in the directory where the key file is located, I ran the command. Is there anything else I'm missing?

The documentation I am following is from https://cloud.google.com/docs/authentication/production#windows

Upvotes: 1

Views: 4064

Answers (1)

DazWilkin
DazWilkin

Reputation: 40091

Couple of things.

gcloud does not use ADC; setting GOOGLE_APPLICATION_CREDENTIALS does not configure gcloud.

You should (have not tried) be able to use gcloud auth activate-service-account ... and then gcloud auth print-access-token.

Or you can just use a regular (human|non-service) account and gcloud auth print-access-token.

Or, you can use gcloud auth application-default login and then gcloud auth application-default print-access-token but the two go together.

Don't quote the token in header:

TOKEN="$(gcloud auth print-access-token)"                     # Either
TOKEN="$(gcloud auth application-default print-access-token)" # Or

curl \
--silent \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
--data @sync-request.json \
https://speech.googleapis.com/v1/speech:recognize

Upvotes: 1

Related Questions