我从这个 使用了谷歌云的自然语言应用编程接口
和我在powershell中使用以下命令:
$env:GOOGLE_APPLICATION_CREDENTIALS="D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json“
然后在cmd:中使用以下命令:
set GOOGLE_APPLICATION_CREDENTIALS=D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json
但是当我使用代码时:
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))
错误消息显示:
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置GOOGLE_APPLICATION_CREDENTIALS或显式创建凭据,然后重新运行应用程序。详情请参考https://developers.google.com/accounts/docs/application-default-credentials。
为什么?
发布于 2018-06-11 08:01:11
我知道错误的原因,我必须使用命令模式来运行代码,请不要运行代码与pycharm或其他IDE.And设置此link接口“启用”。
然后错误就消失了。
发布于 2018-06-07 20:24:58
我认为你必须在你的本地机器上也设置GOOGLE_APPLICATION_CREDENTIALS。下载Google Cloud Shell并在其中设置凭据
https://stackoverflow.com/questions/50717812
复制相似问题