问题
I am trying to build a web page which will translate the text into mp3 using google cloud text-to-speech. After lots of search I found using REST API I can request the API to translate the text into mp3.
I am using JQuery and AJAX for the HTTP request.
Problem is that I am requesting the cloud server to translate the text using following data,
"data" : {
"input": {
"text": encodeURIComponent(text)
},
"voice" : {
"languageCode" : "en-US",
"name" : "en-US-Wavenet-A",
},
"audioConfig" : {
"audioEncoding" : "MP3",
}
},
By sending this I'm getting error 403
which clearly says that I do not have access to perform the requested action, in this document
I know without API key and authorization I cannot access the service. So my questions is how can I send my API key and authorizations keys so that I can get the authorization and perform the requested action.
Edit-1
I am sending request to server using following URL,
https://texttospeech.googleapis.com/v1beta1/text:synthesize?further_parameters_goes_here
If anyone want more information I can give it. Any help will be appreciated.
Thank you in advance.
Regards, Vaibhav M
回答1:
First you need to get your Google Cloud's API Key from your acount (https://cloud.google.com/docs/authentication/api-keys) and try the code bellow, replacing your-api-key in the script:
$( document ).ready(function() {
// An element to play the speech from Google Cloud
var Sound = (function () {
var df = document.createDocumentFragment();
return function Sound(src) {
var snd = new Audio(src);
df.appendChild(snd); // keep in fragment until finished playing
snd.addEventListener('ended', function () {df.removeChild(snd);});
snd.play();
return snd;
}
}());
// The settings for the Ajax Request
var settings = {
"async": true,
"crossDomain": true,
"url": "https://texttospeech.googleapis.com/v1/text:synthesize",
"method": "POST",
"headers": {
"x-goog-api-key": "**your-api-key**",
"content-type": "application/json",
"cache-control": "no-cache",
},
"processData": false,
"data": "{'input':{'text':'I have added the event to your calendar.'},'voice':{'languageCode':'en-gb','name':'en-GB-Standard-A','ssmlGender':'FEMALE'},'audioConfig':{'audioEncoding':'MP3' }}"
}
// The Ajax Request, on success play the speech
$.ajax(settings).done(function (response) {
console.log(response.audioContent);
var snd = Sound("data:audio/wav;base64," + response.audioContent);
});
});
回答2:
I found out two possibilities that can help you.
- Obtain a Bearer token.
If you installed the Cloud SDK, you can easily get the token with this command: gcloud auth application-default print-access-token. It can be executed in the Cloud Shell too. Just be sure that the default user you are logged in has the appropriate role to access the text-to-speech service. Then, attach the token to the header request, for example a final request can look like this.
curl -H "Authorization: Bearer ya29.GqUB8gVkiMCyl2ZCKEfS8Tb9QmS_LRb1bQP__fIPYbCU.....LUAlCRJU9OpFc_hCzSVUwlAZAhac2aZhoh" \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'input: {
'text': 'my custom text'
},
'voice' : {
'languageCode' : 'en-US',
'name' : 'en-US-Wavenet-A'
},
'audioConfig' : {
'audioEncoding' : 'MP3'
}
}" "https://texttospeech.googleapis.com/v1beta1/text:synthesize"
This link integrates the request and the command in one step.
- Obtain an API key.
An API key is more portable than a token, but it can be used by anyone who possess it. It's recommended to restrict such key to the text-to-speech service. Then, you should use the key in the endpoint URL, for example "https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=AIzaSynAJU-EGnhdDaaXH4NVcc". A complete example looks like the following:
curl -H "Content-Type: application/json; charset=utf-8" \
--data "{
'input':{
'text':'my custom text'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}" "https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=AIzaSynAJU-EGnhdDaaXH4NVcc"
回答3:
You need to pass your API key as a header, the header field is "X-Goog-Api-Key". Also make sure you're setting the proper body encoding in the request using the "Content-Type" header, in your case I think it should be "Content-Type: application/json; charset=utf-8". And lastly, I'm think you shouldn't be encoding the text field in the request body.
If you don't have the API key yet, you can follow these steps to get to it
- Create a project (or use an existing one) in the Cloud Console.
- Make sure that billing is enabled for your project.
- Enable the Text-to-Speech API.
- Create an API key.
I'm not familiar with JQuery and AJAX syntax, but you could use this curl command for reference
Curl -H "X-Goog-Api-Key: PUT_YOUR_API_KEY_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/v1beta1/text:synthesize" > synthesize-text.txt
来源:https://stackoverflow.com/questions/50773528/google-cloud-text-to-speech-the-server-responded-with-a-status-of-403