Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an api key in python with google translation api

Tags:

python

sdk

api

I read a couple of hours in the google docs about this but I still have no clue what I'm doing. I basically just want to use the google translation api to translate a few words that I had in mind. I have a valid account with billing details and I tried this code sample from google:


# 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']))

But it gives me this error: https://translation.googleapis.com/language/translate/v2?target=ru&q=Hello%2C+world%21 So I couldn't figute out how to include my API Key in Python, could anyone give me a quick help here, my head is exploding and I think I installed a lot of stuff that I don't need, like the Google Cloud SDK Shell and OAuth library for Python. Cheers

like image 651
Gottfred Meinard Avatar asked Jun 05 '17 23:06

Gottfred Meinard


1 Answers

You need to setup a env variable GOOGLE_APPLICATION_CREDENTIALS with the path to a json file with the api key as explained here https://cloud.google.com/translate/docs/setup?hl=en or set it in your code https://cloud.google.com/docs/authentication/production#passing_code

like image 120
Gonzalo Odiard Avatar answered Oct 13 '22 00:10

Gonzalo Odiard