首页
学习
活动
专区
圈层
工具
发布

Python包
EN

Stack Overflow用户
提问于 2018-02-20 13:35:01
回答 1查看 1.3K关注 0票数 2

我试着运行这段使用谷歌云的代码。

代码语言:javascript
复制
import signal
import sys

from google.cloud import language, exceptions

# create a Google Cloud Natural Languague API Python client
client = language.LanguageServiceClient()

但它提供了以下错误消息:

代码语言:javascript
复制
  Traceback (most recent call last):
  File "analyse-comments.py", line 7, in <module>
    client = language.LanguageServiceClient()
  File "C:\Python27\lib\site-packages\google\cloud\language_v1\gapic\language_service_client.py", line 92, in __init__
    scopes=self._DEFAULT_SCOPES)
  File "C:\Python27\lib\site-packages\google\api_core\grpc_helpers.py", line 132, in create_channel
    credentials, _ = google.auth.default(scopes=scopes)
  File "C:\Python27\lib\site-packages\google\auth\_default.py", line 283, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.

第7行是代码的这一部分

代码语言:javascript
复制
 client = language.LanguageServiceClient()

我已经安装了google和云。我有谷歌的解决方案,但没有一个解决方案适合我的情况下需要解决的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-27 16:49:26

您共享的错误明确指出,凭据存在问题:

代码语言:javascript
复制
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application.

它还邀请您访问文档页以获得有关设置身份验证主题的更多信息:

代码语言:javascript
复制
For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.

这里的具体问题是,您正在使用的客户端库(google.cloud.language)试图在环境变量GOOGLE_APPLICATION_CREDENTIALS中直接找到使用您的GCP帐户进行身份验证的凭据,但无法找到它们。为了解决这个问题,您应该首先从控制台中的服务帐户页下载服务帐户的JSON密钥(单击右边的三个点并创建一个新的JSON键),在本地存储它,然后根据操作系统发行版的不同使用GOOGLE_APPLICATION_CREDENTIALS作为在文件中解释指向它。

一旦使用JSON键的正确目录路径填充了这个环境变量,您使用的客户端库将能够正确地进行身份验证,并且错误应该会消失。

此外,如果该过程对您不起作用(我没有看到任何原因),您可以将凭据文件显式地传递给正在实例化的LanguageServiceClient(),如下面所示,详见自然语言API的API参考

代码语言:javascript
复制
from google.cloud import language
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file('path/key.json')
client = language.LanguageServiceClient(
    credentials=creds,
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48886537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档