Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将身份验证密钥显式传递到Google';s文本到语音引擎?_Python_Authentication_Google Cloud Platform - Fatal编程技术网

Python 如何将身份验证密钥显式传递到Google';s文本到语音引擎?

Python 如何将身份验证密钥显式传递到Google';s文本到语音引擎?,python,authentication,google-cloud-platform,Python,Authentication,Google Cloud Platform,我正在尝试为我的机器人使用谷歌云的文本到语音引擎,但我无法理解前面提到的用Python显式传递密钥的参考页面 昨天我花了几个小时研究如何设置隐式授权所需的环境变量GOOGLE\u APPLICATION\u凭证,包括我用来启动机器人的shell脚本中的export命令,使用Python中的os.environ命令,并使用os.system调用export命令 client=texttospeech.TextToSpeechClient() voice=robot\u config.get('go

我正在尝试为我的机器人使用谷歌云的文本到语音引擎,但我无法理解前面提到的用Python显式传递密钥的参考页面

昨天我花了几个小时研究如何设置隐式授权所需的环境变量
GOOGLE\u APPLICATION\u凭证
,包括我用来启动机器人的shell脚本中的
export
命令,使用Python中的
os.environ
命令,并使用
os.system
调用
export
命令

client=texttospeech.TextToSpeechClient()
voice=robot\u config.get('google\u cloud','voice')
keyFile=robot\u config.get('google\u cloud','key\u file')
hwNum=robot_config.getint('tts','hw_num')
languageCode=robot\u config.get('google\u cloud','language\u code')
voice=texttospeech.types.voiceselection参数(
名字=声音,
语言\代码=语言代码
)
audio_config=texttospeech.types.AudioConfig(
audio_config=texttospeech.enums.AudioEncoding.LINEAR16
)
os.environ['GOOGLE\u应用程序\u凭据]]=keyFile
通过SSH登录表明我已经成功设置了环境变量,因为它显示在
env
;但是,将抛出一个
defaultcredentialerror
,并显示以下消息

无法自动确定凭据。请设置
GOOGLE\u应用程序\u凭据
或显式创建凭据并重新运行应用程序。有关更多信息,请参阅

登录并手动设置环境变量将允许脚本运行和工作,但这不是一个长期解决方案。

这对我来说很有效:

import os
from google.cloud import texttospeech

os.environ ["GOOGLE_APPLICATION_CREDENTIALS"]= "/home/pi/projectx-17f8348743.json"

client=texttospeech.TextToSpeechClient()
这对我很有用:

import os
from google.cloud import texttospeech

os.environ ["GOOGLE_APPLICATION_CREDENTIALS"]= "/home/pi/projectx-17f8348743.json"

client=texttospeech.TextToSpeechClient()

正确答案在
google.oath2
库中。
客户机
对象没有查找json密钥,而是查找服务帐户对象

来自google.oath2导入服务\u帐户
从google.cloud导入文本到语音
client=texttospeech.TextToSpeechClient(
凭据=服务\帐户。凭据。来自\服务\帐户\文件(密钥文件)
)

正确答案在
google.oath2
库中。
客户机
对象没有查找json密钥,而是查找服务帐户对象

来自google.oath2导入服务\u帐户
从google.cloud导入文本到语音
client=texttospeech.TextToSpeechClient(
凭据=服务\帐户。凭据。来自\服务\帐户\文件(密钥文件)
)