The code requires various modules and defines several variables for interacting with the Google API, specifically the Google Drive API. It includes a function called authorizeDrive
that authenticates with the Google API using a credentials file and returns a client instance for the Google Drive API.
npm run import -- "authorize google drive"
var fs = require('fs');
var path = require('path');
var {google} = require('googleapis');
var {GoogleAuth} = require('google-auth-library');
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 GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/drive'
];
function authorizeDrive() {
return new GoogleAuth({
keyFile: credentials,
scopes: GOOGLE_AUTH_SCOPE
}).getClient()
.then(client => google.drive({version: 'v3', auth: client}))
}
module.exports = authorizeDrive;
const fs = require('fs');
const path = require('path');
const { google } = require('googleapis');
const { GoogleAuth } = require('google-auth-library');
// Define constants for profile path and credentials file
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const CREDENTIALS_FILE = './sheet to web-8ca5784e0b05.json';
const GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/drive'
];
// Load credentials file from path
function loadCredentials() {
const credentialsPath = fs.existsSync(CREDENTIALS_FILE)
? path.resolve(CREDENTIALS_FILE)
: path.join(PROFILE_PATH, '.credentials/', CREDENTIALS_FILE);
return fs.readFileSync(credentialsPath, 'utf8');
}
// Load credentials from JSON string
function parseCredentials(credentials) {
return JSON.parse(credentials);
}
// Authorize Google Drive
async function authorizeDrive() {
// Load credentials
const credentialsStr = loadCredentials();
const credentials = parseCredentials(credentialsStr);
// Create GoogleAuth instance
const googleAuth = new GoogleAuth({
keyFile: credentials,
scopes: GOOGLE_AUTH_SCOPE
});
try {
// Get client instance
const client = await googleAuth.getClient();
// Get Google Drive API client
const drive = google.drive({ version: 'v3', auth: client });
return drive;
} catch (error) {
// Handle auth error
console.error('Auth error:', error);
throw error;
}
}
module.exports = authorizeDrive;
Code Breakdown
fs
(File System) for interacting with the file systempath
for working with file pathsgoogleapis
for interacting with the Google APIgoogle-auth-library
for authentication with the Google APIPROFILE_PATH
: the path to the user's home directory, determined by environment variablescredentials
: the path to the credentials file for authentication with the Google APIGOOGLE_AUTH_SCOPE
: an array of scopes for authenticating with the Google Drive API./sheet to web-8ca5784e0b05.json
)credentials
to the resolved path of the filecredentials
to the path of the file in the user's home directory (PROFILE_PATH/.credentials/sheet to web-8ca5784e0b05.json
)authorizeDrive
function returns a new instance of GoogleAuth
with the following options:
keyFile
: the path to the credentials file (credentials
)scopes
: an array of scopes for authenticating with the Google Drive API (GOOGLE_AUTH_SCOPE
)getClient
method of GoogleAuth
to obtain an authenticated clientdrive
method of googleapis
to create a client instance for the Google Drive API, with the authenticated client and version v3