我正在尝试构建一个网页,它将使用谷歌云文本转语音将文本翻译成mp3。经过大量搜索后我发现使用REST API我可以请求API将文本翻译成mp3。
我正在使用JQuery和AJAX来处理HTTP请求。
问题是我请求云服务器使用以下数据翻译文本
"data" : {
"input": {
"text": encodeURIComponent(text)
},
"voice" : {
"languageCode" : "en-US",
"name" : "en-US-Wavenet-A",
},
"audioConfig" : {
"audioEncoding" : "MP3",
}
},
通过发送此消息我error 403
明确表示我无权执行请求的操作,this文档
我知道没有API密钥和授权我无法访问该服务。所以我的问题是如何发送我的API密钥和授权密钥,以便我可以获得授权并执行所请求的操作。
修改-1
我正在使用以下网址向服务器发送请求
https://texttospeech.googleapis.com/v1beta1/text:synthesize?further_parameters_goes_here
如果有人想要更多信息我可以给它。任何帮助将不胜感激。
提前谢谢。
此致 Vaibhav M
答案 0 :(得分:2)
首先,您需要从帐户(https://cloud.google.com/docs/authentication/api-keys)中获取Google Cloud的API密钥,然后尝试下面的代码,在脚本中替换 your-api-key :
$( 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);
});
});
答案 1 :(得分:1)
我发现了两种可以帮助您的可能性。
如果安装了Cloud SDK,则可以使用以下命令轻松获得令牌:gcloud auth application-default print-access-token。它也可以在Cloud Shell中执行。只需确保您登录的默认用户具有适当的角色即可访问文本语音转换服务。然后,将令牌附加到标头请求,例如最终请求可能如下所示。
room_spawns = [[randint(0, 5)] for a in range(4)]
for i in range(0, 4):
these_segments = []
these_shapes = []
if i == 1:
modifier = [800, 0]
elif i == 2:
modifier = [0, 448]
elif i == 3:
modifier = [800, 448]
else:
modifier = [0, 0]
if room_spawns[i] == 0:
these_segments = room_1_segments
these_shapes = room_1_shapes
elif room_spawns[i] == 1:
these_segments = room_2_segments
these_shapes = room_2_shapes
elif room_spawns[i] == 2:
these_segments = room_3_segments
these_shapes = room_3_shapes
elif room_spawns[i] == 3:
these_segments = room_4_segments
these_shapes = room_4_shapes
elif room_spawns[i] == 4:
these_segments = room_5_segments
these_shapes = room_5_shapes
elif room_spawns[i] == 5:
these_segments = room_6_segments
these_shapes = room_6_shapes
for z in range(0, len(these_segments)):
these_segments[z][0] += modifier[0]
these_segments[z][1] += modifier[1]
for x in range(0, len(these_shapes)):
these_shapes[x][0][0] += modifier[0]
these_shapes[x][0][1] += modifier[1]
these_shapes[x][1][0] += modifier[0]
these_shapes[x][1][1] += modifier[1]
segments.append(these_segments)
shapes.extend(these_shapes)
此link一步将请求和命令集成在一起。
API密钥比令牌更可移植,但是拥有它的任何人都可以使用它。建议restrict such key使用文字转语音服务。然后,您应该使用the key in the endpoint URL,例如“ 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"
答案 2 :(得分:0)
您需要将API密钥作为标题传递,标题字段为“X-Goog-Api-Key”。还要确保使用“Content-Type”标头在请求中设置正确的主体编码,在您的情况下我认为它应该是“Content-Type:application / json; charset = utf-8”。最后,我认为你不应该在请求体中对文本字段进行编码。
如果您还没有API密钥,可以按照以下步骤进行操作
我不熟悉JQuery和AJAX语法,但您可以使用此curl命令进行参考
SQLException