google-cloud-platformoauthgoogle-speech-api

Request had invalid authentication credentials. Expected OAuth 2 access token error in cloud speech API


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?


Solution

  • 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