问题
I followed this page:
https://cloud.google.com/text-to-speech/docs/quickstart-protocol
I generated an access token with:
gcloud auth application-default print-access-token
and upon entering this:
curl -H "Authorization: Bearer "$(my_token_is_in_here) \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'input':{
'text':'Android is a mobile operating system developed by Google,
based on the Linux kernel and designed primarily for
touchscreen mobile devices such as smartphones and tablets.'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}" "https://texttospeech.googleapis.com/v1/text:synthesize" > synthesize-text.txt
I got this:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 605 0 297 100 308 297 308 0:00:01 --:--:-- 0:00:01 3517
Buth then I opened up synthesize-text.txt and found
{
"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"
}
}
Clearly gcloud is giving me a token, why doesnt text-to-speech recognize it?
回答1:
[SOLVED]
1) I went to APIs and Services in the 'hamburger' drop down
2) I went to 'credentials' submenu in left column
3) I did 'create credentials' and selected API key then in my request I formulated as
Curl -H "X-Goog-Api-Key: my-newly-created-API-key" -H "Content-Type: application/json; charset=utf-8" --data "{'input':{'text':'Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.'},'voice':{'languageCode':'en-gb','name':'en-GB-Standard-A','ssmlGender':'FEMALE'},'audioConfig':{'audioEncoding':'MP3'}}" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt
now it works.
So it seems the use of 'Authorization Bearer' with the service account key does not work. I am using Windows 10.
回答2:
Like Fredrik has said in his comment, there is an error in Google's Docs and it should be: -H "Authorization: Bearer $(my_token_is_in_here)"
(" at the end, not after Bearer
)
来源:https://stackoverflow.com/questions/52748710/google-cloud-text-to-speech-invalid-api-key