23

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
  • 7,861
  • 33
  • 31
  • 41
Patel
  • 363
  • 1
  • 3
  • 10

5 Answers5

16

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
  • 776
  • 4
  • 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
9

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
  • 2,615
  • 1
  • 24
  • 35
Asbis
  • 150
  • 2
  • 11
7

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

dkb
  • 521
  • 4
  • 14
0

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
  • 15,490
  • 6
  • 67
  • 110
0

There is possibility that you have disabled auth. So

gcloud config set auth/disable_credentials false

manpatha
  • 377
  • 2
  • 7