python - 权限被拒绝 : 403 Request had insufficient authentication scopes

标签 python authentication google-compute-engine text-to-speech google-apis-explorer

为了让 Text-to-Speech API 在 GCE 上通过 Python 脚本工作,我尝试使用在 GCE 下运行的以下代码:

"""Synthesizes speech from the input string of text or ssml.

Note: ssml must be well-formed according to:
    https://www.w3.org/TR/speech-synthesis/
"""

# Authorize server-to-server interactions from Google Compute Engine.
import httplib2
from oauth2client.contrib import gce

credentials = gce.AppAssertionCredentials(
  scope='https://www.googleapis.com/auth/cloud-platform')
http = credentials.authorize(httplib2.Http())

from google.cloud import texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text="This is a test. It is only a test.")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)

# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
    audio_encoding=texttospeech.enums.AudioEncoding.MP3)

# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)

# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
    # Write the response to the output file.
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

我期望一个文件,output.mp3,其中包含“这是一个测试。这只是一个测试。”的演讲。

我收到: “PermissionDenied:403请求的身份验证范围不足。”

我使用了 You-Tube API,并且身份验证有效。我在这里缺少什么?

最佳答案

当您使用 GCE 实例时,请尝试将实例的访问范围设置为“允许完全访问所有云 API”,然后重试。

另请检查您是否正确使用客户端库和服务帐户 key ,如 [2] 中所述

[1] https://cloud.google.com/compute/docs/access/service-accounts?hl=en_US&_ga=2.55324139.-1189275507.1546438047#accesscopesiam

[2] https://cloud.google.com/text-to-speech/docs/reference/libraries

关于python - 权限被拒绝 : 403 Request had insufficient authentication scopes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210257/

相关文章:

.net - 使用 .Net Core Web Api 中的 Azure B2C 处理密码重置

docker-compose - 在Google Cloud Engine中运行docker-compose

python - 使用 argparse 无限输入

php - 在 MySQL 中安全地存储密码,但仍可通过 PAM 访问

python - 有 python3 的 web.py 吗?

spring - 如何在Spring Security 3中设置用户角色?

linux - 更改 chown 主文件夹后无法连接到 SSH 服务器 Google Compute Engine

google-cloud-platform - 为什么需要向 Cloud SQL 服务提供外部 IP 进行授权?

python - 使用 PyYAML 转储程序时如何添加类型标签?

python - Visual Studio,Python 不自动缩进