This code sets up authentication with the Google Sheets API and provides a function, authorizeSheets
, that returns an authorized client for interacting with Google Sheets securely.
npm run import -- "authorize sheets api"
var fs = require('fs');
var path = require('path');
var {GoogleAuth} = require('google-auth-library');
var importer = require('../Core')
var getRpcFromSpec = importer.import("get rpc from spec")
// TODO: recognize a polyfill pattern in APIs and move this to polyfills, combine with aspects to apply it
var google;
try {
google = require('googleapis').google;
} catch (e) {
if(!e.message.includes('Cannot find module')) {
throw e
}
google = {sheets: ({version, auth}) => getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/sheets.${version}.json`)), auth)}
}
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/spreadsheets'
];
function authorizeSheets() {
return new GoogleAuth({
keyFile: credentials,
scopes: GOOGLE_AUTH_SCOPE
}).getClient(/* options here are always ignored b/c cache */)
.then(client => google.sheets({version: 'v4', auth: client}))
}
module.exports = authorizeSheets;
// Import required modules
const fs = require('fs');
const path = require('path');
const { GoogleAuth } = require('google-auth-library');
const importer = require('../Core');
const getRpcFromSpec = importer.import('get rpc from spec');
const googleapis = require('googleapis');
// Define constants
const DEFAULT_PROFILE_PATH = [
process.env.HOME,
process.env.HOMEPATH,
process.env.USERPROFILE,
].find(Boolean) || process.cwd();
const DEFAULT_CREDENTIALS_FILE = './sheet to web-8ca5784e0b05.json';
const DEFAULT_GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/spreadsheets',
];
// Define the Google API wrapper
class GoogleApiWrapper {
constructor(credentials, scope) {
this.credentials = credentials;
this.scope = scope;
}
async authorize() {
try {
const client = await new GoogleAuth({
keyFile: this.credentials,
scopes: this.scope,
}).getClient();
return googleapis.google.sheets({ version: 'v4', auth: client });
} catch (e) {
if (e.message.includes('Cannot find module')) {
// Use the custom implementation if the module is not found
return getRpcFromSpec(require(path.join(__dirname, `../Resources/APIs/sheets.v4.json`)));
}
throw e;
}
}
}
// Define the main function
async function authorizeSheets() {
const credentials = fs.existsSync(DEFAULT_CREDENTIALS_FILE)
? path.resolve(DEFAULT_CREDENTIALS_FILE)
: path.join(DEFAULT_PROFILE_PATH, '.credentials', DEFAULT_CREDENTIALS_FILE);
await authorizeSheetsWithCredentials(credentials);
}
async function authorizeSheetsWithCredentials(credentials) {
const googleAuthScope = DEFAULT_GOOGLE_AUTH_SCOPE;
const googleApiWrapper = new GoogleApiWrapper(credentials, googleAuthScope);
return googleApiWrapper.authorize();
}
module.exports = authorizeSheets;
This code snippet sets up authentication with Google Sheets API and provides a function to obtain an authorized client.
Here's a breakdown:
Imports:
Google API Initialization:
googleapis
library. If unavailable, it falls back to a custom implementation using a locally stored JSON specification for the Sheets API.Credentials Loading:
Authentication Setup:
authorizeSheets
that:
Purpose:
This code provides a reusable function authorizeSheets
to authenticate with the Google Sheets API, allowing other parts of the application to access and interact with Google Sheets data securely.