Google Cloud Client,报google.api_core.exceptions.Forbidden: 403错误

发布于 2022-09-07 23:08:20 字数 2237 浏览 16 评论 0

来自Google Cloud Client Libraries官方文档:https://googlecloudplatform.g...

from google.cloud import translate
client = translate.Client()
client.translate(['Me llamo Jeff', 'My name is Jeff'],target_language='de')

代码执行结果:

C:\Python27\lib\site-packages\google\auth\_default.py:66: UserWarning: Your application has authenticated using end user
 credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your app
lication continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled"
 error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.
  warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
Traceback (most recent call last):
  File "C:\Code\Google\test\run.py", line 15, in <module>
    target_language=target)
  File "C:\Python27\lib\site-packages\google\cloud\translate_v2\client.py", line 233, in translate
    method='POST', path='', data=data)
  File "C:\Python27\lib\site-packages\google\cloud\_http.py", line 293, in api_request
    raise exceptions.from_http_response(response)
google.api_core.exceptions.Forbidden: 403 POST https://translation.googleapis.com/language/translate/v2: Your applicatio
n has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported b
y the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more inform
ation about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.

而如果在cmd下执行的话,可以正常执行

>>> from google.cloud import translate
>>> client = translate.Client()
>>> client.translate(['Me llamo Jeff', 'My name is Jeff'],target_language='de')
[{u'translatedText': u'Mein Name ist Jeff', 'input': 'Me llamo Jeff', u'detected
SourceLanguage': u'es'}, {u'translatedText': u'Mein Name ist Jeff', 'input': 'My
 name is Jeff', u'detectedSourceLanguage': u'en'}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文