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.
npm run import -- "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;
/**
* 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:
Credentials Path:
sheet to web-8ca5784e0b05.json
) based on environment variables and common locations.Dependencies:
GoogleAuth
class from the google-auth-library
package.Authentication Scope:
https://www.googleapis.com/auth/cse
).authorizeSearch
Function:
GoogleAuth
instance using the specified credentials file and scope.Export:
authorizeSearch
function is exported as a module, allowing other parts of the application to use it for authentication.