-1

I am getting a "Request had invalid authentication credentials" error. This is what I did:

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

on a windows machine.

doing "gcloud auth application-default print-access-token" gives me a response. I also set GOOGLE_APPLICATION_CREDENTIALS.

What is wrong here?

1 Answers1

1

Have a look at the documentation, it provides an example using curl:

https://cloud.google.com/speech-to-text/docs/quickstart-protocol

You need to create a Project and a Service Account belonging to the project in order to bind your requests to a potentially billable resource.

You can then use Application Default Credentials for the Service Account, with curl, you'll need to gcloud auth application-default login (using the Service Account) and then gcloud auth application-default print-access-token to get a bearer token for curl.

2021-01-11 Update

Here are the full steps:

PROJECT=[[YOUR-PROJECT]]
BILLING=[[YOUR-BILLING]]
ACCOUNT=[[YOUR-ACCOUNT]] # e.g. roboto
ADDRESS=${ROBOT}@${PROJECT}.iam.gserviceaccount.com

gcloud projects create ${PROJECT}

gcloud beta billing projects link ${PROJECT} \
--billing-account=${BILLING}

# Enable speech-to-text service
gcloud services enable speech.googleapis.com \
--project=${PROJECT}

# Create Service Account
gcloud iam service-accounts create ${ACCOUNT} \
--project=${PROJECT}

# NB The Account needs no roles|permissions

# Create Service Account Key
gcloud iam service-accounts keys create ./${ROBOT}.json \
--iam-account=${EMAIL} \
--project=${PROJECT}

# Export for Application Default Creds
export GOOGLE_APPLICATION_CREDENTIALS="${PWD}/${ROBOT}.json"

# Call it
TOKEN=$(gcloud auth application-default print-access-token)
curl \
--silent \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
https://speech.googleapis.com/v1/speech:recognize \
--data '{ "config": { "encoding":"FLAC", "sampleRateHertz": 16000, "languageCode": "en-US", "enableWordTimeOffsets": false }, "audio": { "uri":"gs://cloud-samples-tests/speech/brooklyn.flac" } }'

Yields:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.9823954
        }
      ]
    }
  ]
}
DazWilkin
  • 32,823
  • 5
  • 47
  • 88