0

我从这个链接使用谷歌云的自然语言 API :

我在 powershell 中使用这个命令:

$env:GOOGLE_APPLICATION_CREDENTIALS="D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json"

在此处输入图像描述

然后我在cmd中使用这个命令:

设置 GOOGLE_APPLICATION_CREDENTIALS=D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json

在此处输入图像描述

但是当我使用python代码时:

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

# Instantiates a client
client = language.LanguageServiceClient()
# The text to analyze
text = u'Hello, world!'
document = types.Document(
    content=text,
    type=enums.Document.Type.PLAIN_TEXT)
# Detects the sentiment of the text
sentiment = client.analyze_sentiment(document=document).document_sentiment
print('Text: {}'.format(text))
print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))

错误信息说:

引发 exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式创建凭据并重新运行应用程序。有关更多信息,请参阅 https://developers.google.com/accounts/docs/application-default-credentials

为什么?

4

2 回答 2

0

我认为您还必须在本地计算机中设置 GOOGLE_APPLICATION_CREDENTIALS 。下载 Google Cloud Shell 并在那里设置凭据

于 2018-06-07T12:24:57.547 回答
0

我知道错误原因,我必须使用cmd模式运行代码,请不要使用pycharm或其他IDE运行代码。并将此链接API设置为“启用”。

然后错误消失了。

于 2018-06-11T00:01:10.667 回答