google.auth.exceptions.DefaultCredentialsError:
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']))