main.js
const { google } = require('googleapis')
, yaml = require('yaml')
, authorize = require('./functions/authorize')
, { readFileSync } = require('fs');
const credentials = yaml.parse(readFileSync('./credentials.yml').toString()).web;
const OAuth2Client = new google.auth.OAuth2({
clientId: credentials.client_id,
clientSecret: credentials.client_secret,
redirectUri: credentials.redirect_uris[0]
});
async function main() {
let tokens = await authorize(OAuth2Client);
console.log(await google.script({
version: 'v1',
auth: OAuth2Client
}).scripts.run({
scriptId: /* script_id */''
}));
}
main();
authorize.js
const { readFileSync, writeFileSync, existsSync } = require('fs')
, inquirer = require('inquirer')
, open = require('open')
, yaml = require('yaml');
module.exports = async function (auth) {
let tokens = existsSync('./tokens.yml') && yaml.parse(readFileSync('./tokens.yml').toString());
if (tokens && tokens.access_token && tokens.refresh_token) {
auth.setCredentials(tokens)
return tokens;
}
let URL = auth.generateAuthUrl({
access_type: 'offline',
scope: [
'https://www.googleapis.com/auth/forms'
]
});
let { in_browser } = await inquirer.prompt({
type: 'confirm',
name: 'in_browser',
message: 'Open the URL in browser?'
});
if (in_browser) open(URL);
else console.log(`Open this URL to authorize this script: ${URL}`);
let { code } = await inquirer.prompt({
type: 'input',
name: 'code',
message: 'Enter the code you got now:'
});
let result = await auth.getToken(code);
auth.setCredentials(result.tokens)
writeFileSync('./tokens.yml', yaml.stringify(result.tokens));
return result.tokens;
}
ScriptId правильный, это точно
google.script(...).projects.get({scriptId:...}) работает вроде, а вот google.script(...).scripts.run({scriptId:...}) не работает (выдает ошибку из названия)