/** * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* eslint-disable camelcase */ // [START apps_script_api_quickstart] const fs = require('fs').promises; const path = require('path'); const process = require('process'); const {authenticate} = require('@google-cloud/local-auth'); const {google} = require('googleapis'); // If modifying these scopes, delete token.json. const SCOPES = ['https://www.googleapis.com/auth/script.projects']; // The file token.json stores the user's access and refresh tokens, and is // created automatically when the authorization flow completes for the first // time. const TOKEN_PATH = path.join(process.cwd(), 'token.json'); const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json'); /** * Reads previously authorized credentials from the save file. * * @return {Promise} */ async function loadSavedCredentialsIfExist() { try { const content = await fs.readFile(TOKEN_PATH); const credentials = JSON.parse(content); return google.auth.fromJSON(credentials); } catch (err) { return null; } } /** * Serializes credentials to a file compatible with GoogleAuth.fromJSON. * * @param {OAuth2Client} client * @return {Promise} */ async function saveCredentials(client) { const content = await fs.readFile(CREDENTIALS_PATH); const keys = JSON.parse(content); const key = keys.installed || keys.web; const payload = JSON.stringify({ type: 'authorized_user', client_id: key.client_id, client_secret: key.client_secret, refresh_token: client.credentials.refresh_token, }); await fs.writeFile(TOKEN_PATH, payload); } /** * Load or request or authorization to call APIs. * */ async function authorize() { let client = await loadSavedCredentialsIfExist(); if (client) { return client; } client = await authenticate({ scopes: SCOPES, keyfilePath: CREDENTIALS_PATH, }); if (client.credentials) { await saveCredentials(client); } return client; } /** * Creates a new script project, upload a file, and log the script's URL. * @param {google.auth.OAuth2} auth An authorized OAuth2 client. */ async function callAppsScript(auth) { const script = google.script({version: 'v1', auth}); let res = await script.projects.create({ resource: { title: 'My Script', }, }); res = await script.projects.updateContent({ scriptId: res.data.scriptId, auth, resource: { files: [ { name: 'hello', type: 'SERVER_JS', source: 'function helloWorld() {\n console.log("Hello, world!");\n}', }, { name: 'appsscript', type: 'JSON', source: '{"timeZone":"America/New_York","exceptionLogging":' + '"CLOUD"}', }, ], }, }); console.log(`https://script.google.com/d/${res.data.scriptId}/edit`); } authorize().then(callAppsScript).catch(console.error); // [END apps_script_api_quickstart]