google search api | test custom search | | Search

This code configures authentication with the Google Custom Search Engine API by locating credentials and defining the necessary scope, providing a function to obtain an authorized client for API requests.

Run example

npm run import -- "authorize custom search"

authorize custom search

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var credentials;
if(fs.existsSync('./sheet to web-8ca5784e0b05.json')) {
    credentials = path.resolve('./sheet to web-8ca5784e0b05.json');
} else {
    credentials = path.join(PROFILE_PATH, '.credentials/sheet to web-8ca5784e0b05.json');
}

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

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

module.exports = authorizeSearch;

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 {string} PROFILE_PATH - The path to the user's profile directory.
 * @const {array} GOOGLE_AUTH_SCOPE - The scope of authentication.
 */
const PROFILE_PATH = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) || process.env.HOMEDRIVE + process.env.HOMEPATH;
const GOOGLE_AUTH_SCOPE = ['https://www.googleapis.com/auth/cse'];

/**
 * Define a function to get the credentials file path.
 * @returns {string} The path to the credentials file.
 */
function getCredentialsFilePath() {
    // Check if the credentials file exists in the current directory.
    if (fs.existsSync('./sheet to web-8ca5784e0b05.json')) {
        // If it exists, return the resolved path.
        return path.resolve('./sheet to web-8ca5784e0b05.json');
    } else {
        // If it doesn't exist, return the path to the file in the user's profile directory.
        return path.join(PROFILE_PATH, '.credentials/sheet to web-8ca5784e0b05.json');
    }
}

/**
 * Define a function to authorize search.
 * @returns {GoogleAuth} The authorized Google auth instance.
 */
function authorizeSearch() {
    // Get the credentials file path.
    const credentials = getCredentialsFilePath();

    // Check if the credentials file exists.
    if (!fs.existsSync(credentials)) {
        throw new Error('Credentials file not found.');
    }

    // Return the authorized Google auth instance.
    return new GoogleAuth({
        keyFile: credentials,
        scopes: GOOGLE_AUTH_SCOPE
    }).getClient();
}

/**
 * Export the authorizeSearch function.
 */
module.exports = authorizeSearch;

This code snippet sets up authentication with the Google Custom Search Engine API.

Here's a breakdown:

  1. Credentials Path:

  2. Dependencies:

  3. Authentication Scope:

  4. authorizeSearch Function:

  5. Export: