These are the code samples found in the language-oriented post covering both GCP APIs.
App | Platform | Credentials | Client libraries | Auth libraries |
---|---|---|---|---|
transnatlang-svcacct-gcp.py |
Python 2 & 3 | ADC (service account or OAuth client ID) | Product | Current/new |
transnatlang-svcacct-gcp-async.py |
Python 3 | ADC (service account or OAuth client ID) | Product | Current/new |
transnatlang-svcacct-gcp.js |
Node.js (CommonJS) | ADC (service account or OAuth client ID) | Product | Current/new |
transnatlang-svcacct-gcp.mjs |
Node.js (modern JS / ECMAScript module) | ADC (service account or OAuth client ID) | Product | Current/new |
transnatlang-apikey-old.py |
Python 2 & 3 | API key | Platform | Old |
Product client libraries are those created for each (GCP) product. By default, these client libraries use Application Default Credentials (ADC) to access GCP APIs. Depending on the execution environment, ADC will either point to service account or OAuth client ID credentials.
Platform client libraries are older and meant to support Google APIs from multiple product families. The blog post features samples that primarily use the former, but the Python API key version requires use of the older platform libraries because API keys aren't fully supported by the (Python) Translation API product client library.
All samples run on Python 3. The primary samples also run on Python 2 to help remaining 2.x users migrate. A fully modernized Python 3 version is available featuring asynchronous execution, f-strings, and function & variable type annotations.
The Node.js samples come in CommonJS and modern ECMAscript module versions. They were built using Node 21 and should work but are untested for older releases.
To learn the code and run the sample scripts from the blog post in your development environment, you're more likely to use the latter credentials type (OAuth client ID) by running the gcloud auth application-default login
command to obtain user authorized credentials. When you're ready to move towards production and want to switch to impersonating a service account, you'd run the gcloud auth application-default login --impersonate-service-account SERVICE_ACCT_EMAIL
command. Learn more at the ADC pages in the docs, starting with the ADC setup page. Also see the page on the different authentication methods and credentials types.
While API keys are the easiest of all the credentials to get started with and implement, they are the least secure of all three credentials types used by Google APIs, so support for API keys isn't always complete (because their use is generally not recommended). The platform client library and older auth libraries are required for the API key version of the sample app because API keys are unsupported by the Python Translation API (product) client libraries even though they are supported by the Python Natural Language API (product) client library. (NOTE: API key support in the Node.js (product or platform) client libraries is unknown -- if anyone has a working example, please file an issue and submit a PR.)
This code sample is only for (typically) non-GCP users who want to experiment with the APIs without the complexities of ADC, service accounts or OAuth client IDs. To run the API key sample, create an API key in the DevConsole and assign it to a variable named API_KEY
in a settings.py
file accessible to the script. More information including detailed steps are available in the blog post.
When compared to the standard version, product client libraries and ADC vs. platform client library and API key, you'll note there many similarities: