google cloud api | invalidate cdn cache | setup sheet backend | Search

This JavaScript code authorizes access to Google APIs by resolving the path to a credentials file, using the Google Auth library to authenticate, and exporting a function to obtain a Google client instance. The authorization function returns a Google client instance with specific scopes.

Run example

npm run import -- "authorize google service"

authorize google service

var fs = require('fs');
var path = require('path');
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var credentials;
if(fs.existsSync('./spahaha-ea443-a78ab2269985.json')) {
    credentials = path.resolve('./spahaha-ea443-a78ab2269985.json');
} else {
    credentials = path.join(PROFILE_PATH, '.credentials/spahaha-ea443-a78ab2269985.json');
}

var {GoogleAuth} = require('google-auth-library');
var GOOGLE_AUTH_SCOPE = [
    'https://www.googleapis.com/auth/compute',
    'https://www.googleapis.com/auth/cloud-platform'
];

function authorizeGoogle() {
    return new GoogleAuth({
        keyFile: credentials,
        scopes: GOOGLE_AUTH_SCOPE
    }).getClient(/* options here are always ignored b/c cache */)
}

module.exports = authorizeGoogle;

What the code could have been:

// Import required modules
const fs = require('fs');
const path = require('path');
const { GoogleAuth } = require('google-auth-library');

// Define constants
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const SCOPE = [
  'https://www.googleapis.com/auth/compute',
  'https://www.googleapis.com/auth/cloud-platform'
];

/**
 * Resolves the path to the credentials file.
 * 
 * If the file exists in the current directory, it uses that. Otherwise, it
 * resolves the path to the user's home directory and appends the credentials file path.
 * 
 * @returns {string} - The resolved path to the credentials file.
 */
function resolveCredentialsPath() {
  if (fs.existsSync('./spahaha-ea443-a78ab2269985.json')) {
    return path.resolve('./spahaha-ea443-a78ab2269985.json');
  }
  return path.join(PROFILE_PATH, '.credentials/spahaha-ea443-a78ab2269985.json');
}

/**
 * Authorizes with Google using the provided credentials file.
 * 
 * @returns {Promise} - A promise resolving to a GoogleAuth client instance.
 */
async function authorizeGoogle() {
  const credentials = resolveCredentialsPath();
  const auth = new GoogleAuth({
    keyFile: credentials,
    scopes: SCOPE
  });
  return auth.getClient();
}

module.exports = authorizeGoogle;

Code Breakdown

Requires and Variable Declarations

var fs = require('fs'); // File system module
var path = require('path'); // Path module
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; // Environment variable for user profile path
var credentials; // Variable to store credentials file path

File Path Resolution

if (fs.existsSync('./spahaha-ea443-a78ab2269985.json')) {
    credentials = path.resolve('./spahaha-ea443-a78ab2269985.json'); // Use local file if exists
} else {
    credentials = path.join(PROFILE_PATH, '.credentials/spahaha-ea443-a78ab2269985.json'); // Use file in user profile directory if not found locally
}

Google Auth Library and Scope Definition

var {GoogleAuth} = require('google-auth-library'); // Google Authentication library
var GOOGLE_AUTH_SCOPE = [
    'https://www.googleapis.com/auth/compute', // Compute Engine scope
    'https://www.googleapis.com/auth/cloud-platform' // Cloud Platform scope
];

Authorization Function

function authorizeGoogle() {
    return new GoogleAuth({
        keyFile: credentials, // Path to credentials file
        scopes: GOOGLE_AUTH_SCOPE // Scope for Google Auth
    }).getClient(/* options here are always ignored b/c cache */) // Get client instance
}

Module Exports

module.exports = authorizeGoogle; // Export authorization function as module