尝试使用Google Cloud Speech API时抛出403 [英] Google cloud speech api throwing 403 when trying to use it

查看:104
本文介绍了尝试使用Google Cloud Speech API时抛出403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将python与Google Cloud Speech API结合使用,我完成了

I'm using python with google cloud speech api I did all the steps in "How to use google speech recognition api in python?" on ubuntu and on windows as well and when I trying to run the simple script from here - "https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/api/speech_rest.py"

我收到下一个错误: <HttpError 403 when requesting https://speech.googleapis.com/$discovery/rest?version=v1beta1 returned "Google Cloud Speech API has not been used in project google.com:cloudsdktool before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=google.com:cloudsdktool then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

I get the next error: <HttpError 403 when requesting https://speech.googleapis.com/$discovery/rest?version=v1beta1 returned "Google Cloud Speech API has not been used in project google.com:cloudsdktool before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=google.com:cloudsdktool then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

奇怪的是我没有名为"cloudsdktool"的项目

what is weird is that I don't have project by the name "cloudsdktool"

我运行"gcloud init",并用"gcloud auth activate-service-account --key-file = jsonfile"命令链接了创建服务帐户密钥时得到的json文件, 我在Linux中尝试创建google凭证环境变量,但仍然得到相同的提示

I run "gcloud init", and linked the json file that I got when I created service account key with "gcloud auth activate-service-account --key-file=jsonfile" command, I tried in linux to create google credentials environment variable and still I get the same massage

推荐答案

所以我找到了两种方法来解决该问题:

So I found two ways to fix that problem:

1-如果使用Google Cloud sdk且云语音为beta版本,则需要运行"gcloud beta init"而不是"gcloud init",然后提供json文件

1 - if using google cloud sdk and the cloud speech is in beta version you need to run 'gcloud beta init' instead of 'gcloud init' and then provide the json file

2-如果您不想使用Google的cloud sdk,则可以直接在python应用程序中传递json文件

2 - if you don't want to use the cloud sdk from google you can pass the json file straight in python app

以下是实现此目的的方法:

here are the methods for this:

from oauth2client.client import GoogleCredentials

GoogleCredentials.from_stream('path/to/your/json')

然后,您仅在凭据上创建范围并进行授权,或者,如示例中一样,如果使用grpc(streaming),则将其传递给标头.

then you just create scope on the creds and authorizing or if using grpc(streaming) you pass it to the header just like in the example.

这是grpc更改后的脚本:

here are the changed script for the grpc:

def make_channel(host, port):
    """Creates an SSL channel with auth credentials from the environment."""
    # In order to make an https call, use an ssl channel with defaults
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)

    # Grab application default credentials from the environment
    creds = GoogleCredentials.from_stream('path/to/your/json').create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = (
        'Authorization',
        'Bearer ' + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(
        lambda _, cb: cb([auth_header], None),
        name='google_creds')

    # compose the two together for both ssl and google auth
    composite_channel = implementations.composite_channel_credentials(
        ssl_channel, auth_plugin)

    return implementations.secure_channel(host, port, composite_channel)

这篇关于尝试使用Google Cloud Speech API时抛出403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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