Google云自然语言API错误:google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据 [英] Google cloud Natural Language API error:google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials
问题描述
我从链接:
I use Google cloud's Natural Language API from this link:
我在powershell中使用此命令:
$ env:GOOGLE_APPLICATION_CREDENTIALS =" D:\ analyze_sentiment \ MyFirstProject-bbe4f7bccb98.json"
$env:GOOGLE_APPLICATION_CREDENTIALS="D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json"
然后我在cmd中使用此命令:
设置 GOOGLE_APPLICATION_CREDENTIALS = D:\ analyze_sentiment \ MyFirstProject-bbe4f7bccb98.json
set 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))
错误消息说:
引发异常.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError:无法 自动确定凭据.请设定 GOOGLE_APPLICATION_CREDENTIALS或显式创建凭据和 重新运行该应用程序.有关更多信息,请参见 https://developers.google.com/accounts/docs/application-default-凭据.
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.
为什么?
推荐答案
我知道错误原因,我必须使用cmd模式运行代码,请不要使用pycharm或其他IDE运行代码. href ="https://console.cloud.google.com/apis/library/language.googleapis.com/" rel ="nofollow noreferrer">链接 API启用".
I know the error reason,I must use cmd mode to run the code,please do not run the code with pycharm or other IDE.And set this link API "enable".
然后错误消失了.
这篇关于Google云自然语言API错误:google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!