python - PermissionDenied:403 请求的身份验证范围不足
问题描述
为了让 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”,然后重试。
还要检查您是否按照 [2] 中的说明正确使用客户端库和服务帐户密钥
[2] https://cloud.google.com/text-to-speech/docs/reference/libraries
推荐阅读
- mysql - Grafana SQL 查询,将字符串转换为数字
- node.js - 如何从 docker-compose.yml 获取端口或环境变量
- javascript - 基于滚动/元素偏移的缩放/旋转元素
- macos - 如何在目标文件中打印函数的二进制代码?
- c++ - OpenCV 单像素色彩空间转换 BGR 到 CieLAB
- notifications - Spring boot admin 2.0.4 电子邮件通知不适用于基本配置
- python - 模板中未读取上下文
- java - 通过反射在单元测试中找到参数化方法
- oauth-2.0 - 在 Microsoft Graph Api 中使用授权类型密码设置范围
- javascript - 正则表达式查找包含这些字母的单词