Google api Google云文本到语音:服务器响应状态为403()
我正试图建立一个网页,将文字翻译成mp3使用谷歌云文字语音。在使用RESTAPI进行了大量搜索之后,我可以请求API将文本翻译成mp3 我正在使用JQuery和AJAX进行HTTP请求 问题是我请求云服务器使用以下数据翻译文本Google api Google云文本到语音:服务器响应状态为403(),google-api,google-cloud-platform,text-to-speech,google-text-to-speech,Google Api,Google Cloud Platform,Text To Speech,Google Text To Speech,我正试图建立一个网页,将文字翻译成mp3使用谷歌云文字语音。在使用RESTAPI进行了大量搜索之后,我可以请求API将文本翻译成mp3 我正在使用JQuery和AJAX进行HTTP请求 问题是我请求云服务器使用以下数据翻译文本 "data" : { "input": { "text": encodeURIComponent(text) },
"data" : {
"input": {
"text": encodeURIComponent(text)
},
"voice" : {
"languageCode" : "en-US",
"name" : "en-US-Wavenet-A",
},
"audioConfig" : {
"audioEncoding" : "MP3",
}
},
通过发送此消息,我得到了错误403
,它清楚地表明我无权执行文档中请求的操作
我知道没有API密钥和授权,我无法访问该服务。所以我的问题是如何发送API密钥和授权密钥,以便获得授权并执行请求的操作
编辑-1
我正在使用以下URL向服务器发送请求
https://texttospeech.googleapis.com/v1beta1/text:synthesize?further_parameters_goes_here
如果有人想要更多的信息,我可以提供。任何帮助都将不胜感激
先谢谢你
问候,,
Vaibhav M您需要将API密钥作为标头传递,标头字段为“X-Goog-API-key”。还要确保使用“Content-Type”头在请求中设置了正确的body编码,在您的情况下,我认为应该是“Content-Type:application/json;charset=utf-8”。最后,我认为您不应该在请求正文中对文本字段进行编码 如果您还没有API密钥,那么可以按照以下步骤进行操作
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
您需要将API密钥作为标头传递,标头字段为“X-Goog-API-key”。还要确保使用“Content-Type”头在请求中设置了正确的body编码,在您的情况下,我认为应该是“Content-Type:application/json;charset=utf-8”。最后,我认为您不应该在请求正文中对文本字段进行编码 如果您还没有API密钥,那么可以按照以下步骤进行操作
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
我发现了两种可能对你有帮助
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"
这将在一个步骤中集成请求和命令
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"
我发现了两种可能对你有帮助
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"
这将在一个步骤中集成请求和命令
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"
首先,您需要从acount()获取Google Cloud的API密钥,并尝试下面的代码,替换脚本中的API密钥:
$( 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);
});
});
首先,您需要从acount()获取Google Cloud的API密钥,并尝试下面的代码,替换脚本中的API密钥:
$( 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);
});
});
谢谢你宝贵的时间。首先,我在你告诉我的谷歌云文档(即“X-Goog-Api-Key”)中没有发现任何这样的标题字段。它是用在卷发上的吗?第二个是您从
-H中给出的格式“内容类型:
到“音频编码”:“MP3”
与我正在使用的相同。第三,我使用encodeURIComponent对文本进行编码。我也有API密钥,但问题是我不知道如何在发送的HTTP请求中使用它。如果您需要任何代码,我可以发布。@VaibhavMandlik“X-Goog-Api-Key”HTTP头可以用于任何Google Cloud Api,请参阅文档。同样,不要对请求数据中的文本进行编码,否则会得到意外的结果。上面的curl命令只是演示如何使用正确格式生成API请求的一种快速方法,您可以在终端中测试它,然后基于它使用JQuery创建您的请求。感谢您的帮助。如你所说,我一定会试试。谢谢你宝贵的时间。首先,我在你告诉我的谷歌云文档(即“X-Goog-Api-Key”)中没有发现任何这样的标题字段。它是用在卷发上的吗?第二个是您从-H中给出的格式“内容类型:
到“音频编码”:“MP3”
与我正在使用的相同。第三,我使用encodeURIComponent对文本进行编码。我也有API密钥,但问题是我不知道如何在发送的HTTP请求中使用它。如果您需要任何代码,我可以发布。@VaibhavMandlik“X-Goog-Api-Key”HTTP头可以用于任何Google Cloud Api,请参阅文档。再说一次,不要