我从这个链接使用谷歌云的自然语言 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。
为什么?