1

I made myself a free account so I can experiment a little bit with Google Cloud Translation API. I generated myself a key from Service account, stored it into a json file, loaded it and everything seems to be working fine. Snippet from my code:

import os
from google.cloud import translate_v2

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"google.json"

print(os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))
translate_client = translate_v2.Client()

text = "Redaksia e IRL-së pas publikimit të skandalit të quajtur “Gjaku i Pistë”, sonte ka dalë me një"
target = 'en'
output = translate_client.translate(text, target_language=target)
print(output)

However, I saw that in Credentials, I can generate an API key and now I'd like to use that API key so I can translate and avoid the json file. The problem is, I tried to find a way of how can I use this api key but none the less, couldn't find any! I tried to pass the key in the constructor of the Client as translate_client = translate_v2.Client(credentials=API_KEY) but I got the error

ValueError: This library only supports credentials from google-auth-library-python.

Has someone used api key for translation api from google and if so, how? If not, is the json file from service account the only way to authenticate against this service?

anthino12
  • 770
  • 1
  • 6
  • 29

1 Answers1

2

That exact error means that it does not support API keys. The possible methods of authentication are listed on this page.

If your only requirement is to not include a JSON file then you can put the JSON contents in a python dict and use the following method to create a credentials object which you can pass to the instantiation of the translate client.

Note that you should never put this dict in any publicly accessible place (same goes for the API key) as it will allow access to whatever the serviceaccount or API key in question has access to.

Docs

sa_dict = {
  "type": "xxxx",
  "project_id": "xxx",
  "private_key_id": "xxx",
  "private_key": "xxxxxx",
  "client_email": "xxxx@xxxxx.iam.gserviceaccount.com",
  "client_id": "xxxxxxxxxx",
  "auth_uri": "xxxxxx",
  "token_uri": "xxxxxx",
  "auth_provider_x509_cert_url": "xxxxx",
  "client_x509_cert_url": "xxxxx.iam.gserviceaccount.com"
}

credentials = service_account.Credentials.from_service_account_info(sa_dict)
translate_client = translate_v2.Client(credentials=credentials)

You other option would be to switch to the general Google API library, called google-api-python-client but this does not have the same optimizations that the specific google-cloud-translate has.

The github repo for this library has an extremely simple sample here and full documentation here...

Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • Thanks for the answer! My goal here was not to store a json file in my server but rather retrieve info from os.environ. This does make sense, I can store the values from my json as os.environs and then get them while constructing this dict. Can I ask you one more question regarding google-translate, I don't want to open a new thread? Is there a feature to get translation of a word and similar words to it? Like the Google Translate UI does? – anthino12 Aug 09 '22 at 13:15
  • 1
    I don't think so. The [docs](https://cloud.google.com/translate/docs/reference/rpc/google.cloud.translate.v2#translatetextrequest) for the API don't mention synonyms anywhere. Note that the python library is simply a wrapper for this API. It's intended for usage in texts, not single words. The model used behind the scenes selects which word to use. The [newer v3](https://cloud.google.com/translate/docs/reference/rpc/google.cloud.translation.v3) doesn't support synonyms neither, but there are some new features that might be interesting. [Docs](https://cloud.google.com/translate/docs/editions) – Edo Akse Aug 09 '22 at 13:40
  • 1
    You might want to look into specific libraries for synonyms, such as `nltk` – Edo Akse Aug 09 '22 at 13:42