0

I use Google cloud's Natural Language API from this link:

And I use this command in powershell :

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

enter image description here

Then I use this command in cmd:

set GOOGLE_APPLICATION_CREDENTIALS=D:\analyze_sentiment\MyFirstProject-bbe4f7bccb98.json

enter image description here

But when I use python code:

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))

The error message said:

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.

Why?

Community
  • 1
  • 1

2 Answers2

0

I think you have to set GOOGLE_APPLICATION_CREDENTIALS also in you local machine. Download Google Cloud Shell and set there the credentials

Awais
  • 21
  • 6
  • I set GOOGLE_APPLICATION_CREDENTIALS in powershell and cmd.But I run my code with pycharm.When I use cmd to run the code,it does not have this mistake.But every time I get into the cmd, I have to set it once.Is there any way to set it up once? I don't want to set it every time. –  Jun 08 '18 at 01:45
0

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".

And then the error disappeared.

  • If you still want to use Pycharm or other IDE, read this [answer](https://stackoverflow.com/questions/44328277/how-to-auth-to-google-cloud-using-service-account-in-python) – liorko Dec 21 '19 at 21:21