Android studio - Macbook - Google translate - "The request is missing a valid API key"

时间:2019-03-06 11:43:39

标签: android-studio google-cloud-platform key credentials google-translate

I develop my App on Android studio on a Macbook. Trying to use Google Translate API I keep on getting "The request is missing a valid API key".

I have created a Service Account for my app on GCP, created a key and downloaded it to my Macbook.

My ~/.bash_profile is:

eval $(/usr/libexec/path_helper -s) PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
export GOOGLE_APPLICATION_CREDENTIALS=vigilant-sunup-233615-64a91c3335bb.json

The echo looks good also:

Brian-MacBook-Pro:~ Brian$ echo $GOOGLE_APPLICATION_CREDENTIALS vigilant-sunup-233615-64a91c3335bb.json

The concerned code is:

    protected String doInBackground(String... params)
    {
        String text = params[0];        //text to translate
        String sourceLang = "en";   // source Language
        String targetLang = "fr";   // target Language
        String translated = "";

        try
        {
            //update UI


            Translate translate = TranslateOptions.getDefaultInstance().getService();
            Translation translation =
                    translate.translate(
                            text,
                            Translate.TranslateOption.sourceLanguage(sourceLang),
                            Translate.TranslateOption.targetLanguage(targetLang));

            translated =  translation.getTranslatedText();

        } catch (Exception e) {
            e.printStackTrace();
        }

I keep on getting "The request is missing a valid API key" when it reaches the statement:

            Translation translation =
                    translate.translate(
                            text,
                            Translate.TranslateOption.sourceLanguage(sourceLang),
                            Translate.TranslateOption.targetLanguage(targetLang));

Any help ?

Thank you.

0 个答案:

没有答案