samples/defaultauth.js
async function main() {
  // The `getClient` method will choose a service based authentication model
  const auth = await google.auth.getClient({
    // Scopes can be specified either as an array or as a single, space-delimited string.
    scopes: ['https://www.googleapis.com/auth/compute'],
  });

  // Obtain the current project Id
  const project = await google.auth.getProjectId();

  // Get the list of available compute zones for your project
  const res = await compute.zones.list({project, auth});
  console.log(res.data);
}
async function main() {
  // The `getClient` method will choose a service based authentication model
  const auth = await google.auth.getClient({
    // Scopes can be specified either as an array or as a single, space-delimited string.
    scopes: ['https://www.googleapis.com/auth/compute'],
  });

  // Obtain the current project Id
  const project = await google.auth.getProjectId();

  // Get the list of available compute zones for your project
  const res = await compute.zones.list({project, auth});
  console.log(res.data);
}
async function main ( ) { // The `getClient` method will choose a service based authentication model const auth = await google . auth . getClient ( { // Scopes can be specified either as an array or as a single, space-delimited string. scopes : [ 'https://www.googleapis.com/auth/compute' ] , } ) ; // Obtain the current project Id const project = await google . auth . getProjectId ( ) ; // Get the list of available compute zones for your project const res = await compute . zones . list ( { project , auth } ) ; console . log ( res . data ) ; }
The google.auth.getClient method creates the appropriate type of credential client for you,
depending upon whether the client is running in Google App Engine, Google Compute Engine, a
Managed VM, or on a local developer machine. This allows you to write one set of auth code that
will work in all cases. It most situations, it is advisable to use the getClient method rather
than creating your own JWT or Compute client directly.

Note: In order to run on a local developer machine, it is necessary to download a private key
file to your machine, and to set a local environment variable pointing to the location of the
file. Create a service account using the Google Developers Console using the section APIs & Auth.
Select "Generate new JSON key" and download the resulting file. Once this is done, set the
GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the location of the .json file.

See also:
https://developers.google.com/accounts/docs/application-default-credentials
 Get the appropriate type of credential client, depending upon the runtime environment.
The google . auth . getClient method creates the appropriate type of credential client for you depending upon whether the client is running in Google App Engine Google Compute Engine a Managed VM or on a local developer machine . This allows you to write one set of auth code that will work in all cases . It most situations it is advisable to use the getClient method rather than creating your own JWT or Compute client directly .