Google云文字语音转换:服务器响应状态为403() [英] Google cloud text-to-speech : the server responded with a status of 403 ()

查看:129
本文介绍了Google云文字语音转换:服务器响应状态为403()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个网页,该网页将使用Google Cloud Text-to-Speech将文字转换为mp3.经过大量搜索后,我发现使用REST API可以请求API将文本转换为mp3.

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.

我正在将JQuery和AJAX用于HTTP请求.

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",
                    }
               },

通过发送此消息,我得到error 403,它清楚地表明我无权执行请求的操作,位于

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

我知道,如果没有API密钥和授权,我将无法访问该服务.因此,我的问题是如何发送API密钥和授权密钥,以便获得授权并执行请求的操作.

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.

编辑1

我正在使用以下URL向服务器发送请求,

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.

谢谢.

关于, Vaibhav M

Regards, Vaibhav M

推荐答案

首先,您需要从帐户中获取Google Cloud的API密钥(

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);
    });

});

这篇关于Google云文字语音转换:服务器响应状态为403()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆