39

i have followed the google cloud speech api quickstart of requesting api by using

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

and following link but i got error of

{
  "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"
  }
}

What should i do now?

Thanx in advance

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Patel
  • 531
  • 2
  • 5
  • 11

8 Answers8

34

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

Samir Aleido
  • 976
  • 6
  • 12
  • hi i have implemented google cloud vision api and followed this link https://cloud.google.com/vision/docs/how-to upto OCR .API works fine but i want to implement cloud speech api as well . that can return real time audio streaming results in json format...but i am new to GCP.. I AM afraid ..if i set up cloud speech api then does it affect previous vision OCR setup ? – Patel Feb 13 '18 at 11:45
  • It should not affect if you are using the same account for both APIs. – Samir Aleido Feb 13 '18 at 15:01
  • Basically what `gcloud auth application-default login` does is acquiring credentials to make it available to the applications in your local machine to use when they are configured to use **Application Default Credentials** – Samir Aleido Feb 13 '18 at 15:02
  • What package/language you use for your OCR setup? – Samir Aleido Feb 13 '18 at 15:02
  • You can use a service account as explained in the [Vision API docs for Client Libraries](https://cloud.google.com/vision/docs/reference/libraries#setting_up_authentication). – Guillem Xercavins Feb 19 '18 at 10:14
  • thank you now i can access cloud firestore via rest – Akif Kara May 14 '21 at 00:58
16

I fixed the problem by logging out, and logging in to agree to some new terms and conditions google have made since last time I used google clouds (firebase in my case).

Filip
  • 3,002
  • 1
  • 26
  • 37
Asbis
  • 432
  • 5
  • 16
9

Replace $(gcloud auth application-default print-access-token) with what gets printed when you call the command.

dkb
  • 551
  • 5
  • 14
5

Make sure your date and time are correct and also your timezone is correct. This was how I solved this problem

AyoAyomide
  • 71
  • 1
  • 4
1

Samir's solution worked for me.

Just a reminder that if using other software interacting with GCP, you may have to restart software before the auth takes effect.

In my case, when using RStudio, I ran gcloud auth application-default login to authenticate, then command + shift + f10 to restart the R session, and then everything was good to go.

Some packages will have a function that refreshes the token in the current R session. For example the bigrquery library has bq_auth() which adds the new token to the R session (without needing to restart the session etc)

stevec
  • 41,291
  • 27
  • 223
  • 311
1

There is possibility that you have disabled auth. So

gcloud config set auth/disable_credentials false

manpatha
  • 489
  • 4
  • 11
0

If you have changed the scopes assigned to the credentials since the last time you signed-in your client, make sure you sign in again to update the client tokens.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
0

I had this error when using GCP CLI, so unrelated but maybe someone stumbles on this thread same as me.

I ran this command:

gcloud auth application-default login

after that, the error was still there, but then I ran second command

gcloud auth application-default print-access-token

CLI then prompted me to provide password, I did, and after that I was able to use GCP CLI again (I didn't use access token that was returned, simply re-entering password worked for me)

sdooo
  • 1,851
  • 13
  • 20