Reputation: 121
I tried run this code that uses Google cloud.
import signal
import sys
from google.cloud import language, exceptions
# create a Google Cloud Natural Languague API Python client
client = language.LanguageServiceClient()
But it gives the following error message:
Traceback (most recent call last):
File "analyse-comments.py", line 7, in <module>
client = language.LanguageServiceClient()
File "C:\Python27\lib\site-packages\google\cloud\language_v1\gapic\language_service_client.py", line 92, in __init__
scopes=self._DEFAULT_SCOPES)
File "C:\Python27\lib\site-packages\google\api_core\grpc_helpers.py", line 132, in create_channel
credentials, _ = google.auth.default(scopes=scopes)
File "C:\Python27\lib\site-packages\google\auth\_default.py", line 283, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application. For more
information, please see
https://developers.google.com/accounts/docs/application-default-credentials.
and line 7 is this part of code
client = language.LanguageServiceClient()
i have already pip install google and cloud. And i have google for solutions, but none of the solutions fits what needs to solve in my situation.
Upvotes: 2
Views: 1666
Reputation: 8178
The error that you shared clearly states that there is an issue with credentials:
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or
explicitly create credential and re-run the application.
And it invites you to visit the documentation page for Setting Up Authentication more information regarding this topic:
For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.
The specific issue here is that the Client Libraries you are using (google.cloud.language
) are trying to find the credentials to authenticate with your GCP account directly in the environment variable GOOGLE_APPLICATION_CREDENTIALS
but it is not being able to find them. In order to solve this issue, you should first download the JSON key for your Service Account from the Service Accounts page in the Console (clicking on the three points at the right and creating a new JSON key), store it locally, and then point to it using GOOGLE_APPLICATION_CREDENTIALS
, as explained in the documentation, depending on your OS distribution.
Once this environment variable is populated with the right directory path to the JSON key, the Client Library you are using will be able to authenticate properly and the error should disappear.
Also, if that process does not work for you (I do not see any reason why it would not), you can pass the credentials file explicitly to the LanguageServiceClient()
that you are instantiating, like below, as detailed in the API reference for the Natural Language API:
from google.cloud import language
from google.oauth2 import service_account
creds = service_account.Credentials.from_service_account_file('path/key.json')
client = language.LanguageServiceClient(
credentials=creds,
)
Upvotes: 2