Search code examples
amazon-ec2chef-infragoogle-translategoogle-api-nodejs-client

Google Translate service account 403 dailyLimitExceeded error


I'm configuring a Node.JS server to use the Google Translate API. So far, I've done the following:

  1. Set up a Google account
  2. Add credit card and enable billing
  3. Create a project
  4. Enable Google Translate API for project
  5. Create two service accounts for project* and save the JSON key files

*One service account for local development and one account for deployed application.

Sample code (Typescript):

import * as Translate from '@google-cloud/translate';

export async function translate(text: string, to: string): Promise<string> {

    let translation = '';

    const googleTranslate = Translate({
        projectId: PROJECT_ID,
        keyFilename: PATH/TO/KEY_FILE
    });

    const response = await googleTranslate.translate(text, to);

    if (Array.isArray(response[0])) {
        for (let t of response[0]) {
            translation += t;
        }
    }
    else {
        translation = response[0];
    }

    return translation;

}

I tested both the local and dev key on my workstation and successfully translated. However, it does not work in a deployed environment (different machine, dynamic IP). The following error occurs:

Error response:

{
    domain: 'usageLimits',
    reason: 'dailyLimitExceeded',
    message: 'This API requires billing to be enabled on the project. Visit https://console.developers.google.com/billing?project=GOOGLE_PROJECT_ID to enable billing.',
    extendedHelp: 'https://console.developers.google.com/billing?project=project=GOOGLE_PROJECT_ID'
}

Billing is enabled so what am I missing?


Solution

  • Resolved!

    Unescaped characters in the private key were breaking inside a Chef cookbook recipe. A different process now syncs the file to the server on initialization, and translation works.