Home > Software design >  Google Translate API Key json format
Google Translate API Key json format

Time:09-26

I am trying to use the Google translate API from a local machine (not google cloud).

Prior to running the following I have run

export GOOGLE_APPLICATION_CREDENTIALS=API.json

Where the API.json is:

{ "key":"the_key_code" }

After running this code:

import google.auth
from google.cloud import translate
_, PROJECT_ID = google.auth.default()
TRANSLATE = translate.TranslationServiceClient()

I got the following error:

DefaultCredentialsError: The file API_.json does not have a valid type. Type is None, expected one of ('authorized_user', 'service_account', 'external_account')

Then I added type: service_account to the json and got this error:

DefaultCredentialsError: ('Failed to load service account credentials from API.json', ValueError('Service account info was not in the expected format, missing fields client_email, token_uri.'))

I cannot find any documentation as to what the format for the API key json file should be and there is no way to export a json file from google cloud platform, only the ability to copy the key.

Any ideas on how to get translate to work from a local machine?

CodePudding user response:

I had to create a credential file for Analytics API too and faced the same lack of informations. I had no choice and used the new google Cloud Dashboard, you have to create an application, select the API you want, then Google provide you a valid credentials file.

My file looked like this :

{
  "type": "service_account",
  "project_id": "***",
  "private_key_id": "***",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB***xmw0Fcs=\n-----END PRIVATE KEY-----\n",
  "client_email": "***",
  "client_id": "***",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "***"
}

That's why i'm pretty sure you have to do the same with translate API, first declaration "type": "service_account", match with the error you get.

  • Related