gpt4 book ai didi

python - 尝试使用 Google Cloud Speech API 时抛出 403

转载 作者:IT王子 更新时间:2023-10-29 00:34:51 26 4
gpt4 key购买 nike

我将 python 与谷歌云语音 api 一起使用 我在 ubuntu 和 Windows 上执行了“How to use google speech recognition api in python?”中的所有步骤,当我尝试从此处运行简单脚本时 -“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.">

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

我运行“gcloud init”,并使用“gcloud auth activate-service-account --key-file=jsonfile”命令链接我在创建服务帐户 key 时获得的 json 文件,我尝试在 linux 中创建 google credentials 环境变量,但我仍然得到相同的信息

最佳答案

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

1 - 如果使用 google cloud sdk 并且云语音处于测试版,则需要运行“gcloud beta init”而不是“gcloud init”,然后提供 json 文件

2 - 如果你不想使用来自谷歌的云 sdk,你可以直接在 python 应用程序中传递 json 文件

这里有一些方法:

from oauth2client.client import GoogleCredentials

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

然后您只需在 creds 上创建范围并授权,或者如果使用 grpc(streaming),您将它传递给 header ,就像在示例中一样。

这里是 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)

关于python - 尝试使用 Google Cloud Speech API 时抛出 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com