Fran Marzoa
Fran Marzoa

Reputation: 4544

Google Api translate authentication without environment variables

I'm trying to use Google translate API without using environment variables, but it seems I'm missing something. Here's the kotlin code I'm using:

import com.google.auth.oauth2.ServiceAccountCredentials
import com.google.cloud.translate.Translate
import com.google.cloud.translate.TranslateOptions
import java.io.FileInputStream

class StringsTranslator {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            TranslateOptions.newBuilder().credentials = ServiceAccountCredentials.fromStream(
                    FileInputStream("ktools/google-apis-credentials.json")
            )

            val translate = TranslateOptions.getDefaultInstance().service

            val text = "Hello world!"
            val translation = translate.translate(
                    text,
                    Translate.TranslateOption.sourceLanguage("en"),
                    Translate.TranslateOption.targetLanguage("es")
            )

            println("$text = ${translation.translatedText}")
        }
    }
}

I basically took this example and adapted it: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/translate/cloud-client/src/main/java/com/example/translate/QuickstartSample.java

But I'm getting this exception:

Exception in thread "main" com.google.cloud.translate.TranslateException: The request is missing a valid API key.

The file is there as downloaded from google (otherwise it'd throw an IOException before anyway), and it seems the TranslateOptions.newBuilder() has no problems with it, so I think I'm doing something wrong in the middle, or missing some step so those options are used by the translate service further.

TIA!

Upvotes: 1

Views: 1954

Answers (2)

user4985526
user4985526

Reputation:

the first answer is a Java version, but I use Python google-tranlate-client. below is the solution.

def g_translate(self, target_language):
    import os
    credential_path = os.path.dirname(__file__) + '/key.json'
    from google.oauth2 import service_account

    credentials = service_account.Credentials.from_service_account_file(credential_path)
    translate_client = translate.Client(credentials=credentials)

    translation = translate_client.translate(self.local, target_language=target_language)

    return translation['translatedText']

I don't want use env variable cause need to config in all the server env, I guess this way is more convinient on production env

Github Question

Upvotes: 1

Fran Marzoa
Fran Marzoa

Reputation: 4544

Ok, I figured it out just a second after posting the question. I'll leave this code here in the hope it'll be eventually useful for someone else.

import com.google.auth.oauth2.ServiceAccountCredentials
import com.google.cloud.translate.Translate
import com.google.cloud.translate.TranslateOptions
import java.io.FileInputStream

class StringsTranslator {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val translate = TranslateOptions.newBuilder().setCredentials(ServiceAccountCredentials.fromStream(
                    FileInputStream("ktools/google-apis-credentials.json")
            )).build().service

            val text = "Hello world!"
            val translation = translate.translate(
                    text,
                    Translate.TranslateOption.sourceLanguage("en"),
                    Translate.TranslateOption.targetLanguage("es")
            )

            println("$text = ${translation.translatedText}")
        }
    }
}

Upvotes: 4

Related Questions