google.auth.exceptions.DefaultCredentialsError:
Asked Answered
B

4

28

So, I am following the same procedure exactly according to the Google Translate API's Documentation.

The code below was provided in it.

# Imports the Google Cloud client library

from google.cloud import translate

# Instantiates a client
translate_client = translate.Client()

# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'

# Translates some text into Russian
translation = translate_client.translate(
    text,
    target_language=target)

print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))

Now when I run this I am returned with the following error:

Traceback (most recent call last):   File "test.py", line 5, in <module>
    translate_client = translate.Client()   File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\translate_v2\client.py", line 65, in __init__
    super(Client, self).__init__(credentials=credentials, _http=_http)   File "C:\ProgramData\Anaconda3\lib\site-packages\google\cloud\client.py", line 129, in __init__
    credentials, _ = google.auth.default()   File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 294, in default
    credentials, project_id = checker()   File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 165, in _get_explicit_environ_credentials
    os.environ[environment_vars.CREDENTIALS])   File "C:\ProgramData\Anaconda3\lib\site-packages\google\auth\_default.py", line 89, in _load_credentials_from_file
    'File {} was not found.'.format(filename)) google.auth.exceptions.DefaultCredentialsError: File  D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json was not found.

The D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json is the path where I put my key provided by Google Cloud for this project.

How do I resolve this?

Blanketing answered 27/7, 2018 at 8:55 Comment(0)
F
45

Your need to set an environment variable for GOOGLE_APPLICATION_CREDENTIALS

You can add this in your code by adding in the following lines:

credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

Complete solution:

# Imports the Google Cloud client library

import os    
from google.cloud import translate

credential_path = "D:\Summer Projects\Translate\social media analysis-2a59d94ba22d.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

# Instantiates a client
translate_client = translate.Client()

# The text to translate
text = u'Hello, world!'
# The target language
target = 'ru'

# Translates some text into Russian
translation = translate_client.translate(
    text,
    target_language=target)

print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
Folacin answered 27/7, 2018 at 9:5 Comment(1)
Additionally, you can find more information about how to set up authentication for Client Libraries in this documentation link.Brazilin
C
1

If you work on linux, just put this in your python code:

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./Authentication.json"
Caddric answered 31/8, 2022 at 19:49 Comment(0)
H
1

If setting from command prompt:

SET GOOGLE_APPLICATION_CREDENTIALS=C:\work\xxxba6.json 

Note: Do not put quotes in the path "C:\work\xxxba6.json".

Google Python scripts cannot handle it.

Helbonnas answered 2/1, 2023 at 7:21 Comment(0)
P
0

I am now dealing with the vision API. My solution is depend on the answer above.

But the sample code from google is a little bit different with previous code.

For my sample code is

def run_quickstart():
    import os
    import io
    from google.cloud import vision
    from google.cloud.vision import types
    ...

    # I made the modification here
    credential_path = "PATH"

    # for example
    # credential_path = "projectname-xxxxxxxx.json"

    # WRONG code
    # credential_path = "~/Downloads/projectname-xxxxxxxx.json"


    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

The things need to noticed is that I put the JSON file into the same folder as .py
so the PATH is not actually a real path to point the JSON file
It's only goes to the name of the JSON file, like this projectname-xxxxxxxx.json

Polypeptide answered 14/6, 2019 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.