google-cloud-python: language: DefaultCredentialsError: Could not automatically determine credentials

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"

Then I use this command in cmd:

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

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.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 26 (7 by maintainers)

Most upvoted comments

The following solution worked. : explicitly mentioning the location of the private key https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files image

image I cannot completely interpret the output of set, but I assume this is your question. It identifies that google application credentials above path.

set path without quotation marks! set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

wrong: set GOOGLE_APPLICATION_CREDENTIALS=“[PATH]”

This one solved it for me on all platforms:

import os os.environ[“GOOGLE_APPLICATION_CREDENTIALS”] = “FULLPATH TO JSON”

@dapsjj Thanks for the report. In order to help us diagnose, can you please provide:

  • the full tracebak
  • the output of pip --freeze

Also, at the top of your script, can you add the following lines, and include the output:

import os

print('Credendtials from environ: {}'.format(
    os.environ.get('GOOGLE_APPLICATION_CREDENTIALS'))

This is the clue:

Credendtials from environ: None

Your script is not running in a context where it has the GOOGLE_APPLICATION_CREDENTIALS environment variable set.

The following solution worked. : explicitly mentioning the location of the private key https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files image

Just sharing it in plain text so it can be easily copied 😄

` from google.cloud import language from google.cloud.language import enums from google.cloud.language import types from google.oauth2 import service_account import os

# In this case I'm using a relative path from the the same path where this script is located.
credentials_path = os.path.dirname(os.path.realpath(__file__)) + os.sep + '[RELATIVE_PATH_TO_JSON_KEY]'


credentials = service_account.Credentials.from_service_account_file(credentials_path)
client = language.LanguageServiceClient(credentials=credentials)

`

My issues already solved