I'm trying to call the language_v1 API but get the following error message:
google.api_core.exceptions.Unauthenticated: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See
https://developers.google.com/identity/sign-in/web/devconsole-project.
This is my code:
from google.cloud import language_v1
from google.oauth2 import service_account
SCOPES = 'https://www.googleapis.com/auth/cloud-language'
SERVICE_ACCOUNT_FILE = 'service_account.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
client = language_v1.LanguageServiceClient(credentials=credentials)
type_ = language_v1.Document.Type.PLAIN_TEXT
language = "en"
text = "I love life!"
document = {"content": text, "type_": type_, "language": language}
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_sentiment(request={'document': document, 'encoding_type': encoding_type})
score = response.document_sentiment.score
print(score)
- I have ensured that my service account email has the necessary permissions to call the api
- my service account file is also a valid file and contains expected items (private key etc)
What could be the reason for the error?
You're using
from_service_account_file
incorrectly.See the Service Account private key files
It's almost always better to use Application Default Credentials:
And, in this case, because
documents.analyzeSentiment
acceptscloud-platform
as a scope, you don't even need to rescope the credentials: