如何解决400呼叫者的项目与父项目不匹配
我有这段代码块,基本上可以使用云翻译API将文本从一种语言翻译成另一种语言。问题在于此代码始终引发错误:“调用者的项目与父项目不匹配”。可能是什么问题?
translation_separator = "translated_text: "
language_separator = "detected_language_code: "
translate_client = translate.TranslationServiceClient()
# parent = translate_client.location_path(
# self.translate_project_id,self.translate_location
# )
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = (
os.getcwd()
+ "/translator_credentials.json"
)
# Text can also be a sequence of strings,in which case this method
# will return a sequence of results for each text.
try:
result = str(
translate_client.translate_text(
request={
"contents": [text],"target_language_code": self.target_language_code,"parent": f'projects/{self.translate_project_id}/'
f'locations/{self.translate_location}',"model": self.translate_model
}
)
)
print(result)
except Exception as e:
print("error here>>>>>",e)
解决方法
您的问题似乎与您在应用程序上使用的身份验证方法有关,请遵循guide来获取带有翻译API的身份验证方法。如果您尝试使用代码传递凭据,则可以在代码with中明确指向服务帐户文件:
def explicit():
from google.cloud import storage
# Explicitly use service account credentials by specifying the private key
# file.
storage_client = storage.Client.from_service_account_json(
'service_account.json')
此外,还有codelab用于使用Python的翻译API入门,这是逐步使用Python的翻译API的入门指南。
如果问题仍然存在,您可以尝试为Google支持创建一个Public Issue Tracker
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。