Authorize access to Google Cloud Translate from Firebase Cloud Function?

Viewed 27

I'm trying to write a Firebase Cloud Function that calls Google Cloud Translate. I'm getting this error:

Error: 7 PERMISSION_DENIED: Cloud IAM permission 'cloudtranslate.generalModels.predict' denied. 

It appears that my credentials aren't being passed from the Firebase Cloud Function to Google Cloud Translate. I set up a user-managed service account, First I tried this to deploy from the CLI:

firebase deploy --only functions:ENtranslateES --service-account google-cloud-translate@my-awesome-app.iam.gserviceaccount.com

That threw this error:

error: unknown option '--service-account'

Then I tried this:

gcloud functions deploy ENtranslateES --service-account google-cloud-translate@my-awesome-app.iam.gserviceaccount.com

That worked. I got a lengthy response on the CLI without errors and I see in my Google Cloud Console that the Cloud Function ENtranslateES was last deployed at the time that I executed that command.

Triggering the Firebase Cloud Function continues to return the PERMISSION_DENIED: Cloud IAM permission error.

Here's my code:

exports.ENtranslateES = functions.firestore.document('Users/{userID}/English/Translation_Request').onUpdate((change) => { 
    const { TranslationServiceClient } = require('@google-cloud/translate').v3;
    const translationClient = new TranslationServiceClient();
    const projectId = 'my-awesome-app';
    const location = 'global';
    const text = 'Hello, world!';

    async function translateText() {
        const request = {
            parent: `projects/${projectId}/locations/${location}`,
            contents: [text],
            mimeType: 'text/plain', // mime types: text/plain, text/html
            sourceLanguageCode: 'en',
            targetLanguageCode: 'es',
        };

        const [response] = await translationClient.translateText(request);

        for (const translation of response.translations) {
            console.log(`Translation: ${translation.translatedText}`);
        }
    }

    return translateText()

});

I also set up a POST query from Postman to Google Cloud Translate. I entered Authorization properties for Client ID, Client Secret, Auth URL, Access Token URL, etc. The Postman query worked. Should I put my Client ID, Client Secret, etc. in my Firebase Cloud Function code? From what I've read it appears that this is unnecessary if I deploy the function with a service account.

0 Answers
Related