Skip to content

Instantly share code, notes, and snippets.

@nowri
Last active November 9, 2015 08:43
Show Gist options
  • Save nowri/8c3d8ff2dea649b87181 to your computer and use it in GitHub Desktop.
Save nowri/8c3d8ff2dea649b87181 to your computer and use it in GitHub Desktop.
ES6対応 GAS、Execution API実行用認証スクリプト 参考 : https://developers.google.com/apps-script/guides/rest/quickstart/js
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
const CLIENT_ID = 'CHANGE_YOUR_CLIENT_ID';
const SCOPES = ['CHANGE_YOUR_SCOPES'];
const SCRIPT_ID = 'CHANGE_YOUR_SCRIPT_ID';
//callback events
export const COMPLETE = "complete";
export const ERROR = "error";
export default class GasAuth {
constructor(cb) {
this.cb = cb;
this.init();
}
init() {
window.checkAuth = ()=> {
this.auth();
};
window.handleAuthClick = ()=> {
this.handleAuthClick();
};
let script = document.createElement('script');
script.src = "https://apis.google.com/js/client.js?onload=checkAuth";
document.body.appendChild(script);
}
/**
* Check if current user has authorized this application.
*/
auth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, (authResult)=> {
this.handleAuthResult(authResult);
});
}
handleAuthResult(authResult) {
if(authResult && !authResult.error) {
this.cb(COMPLETE);
} else {
this.cb(ERROR);
}
}
handleAuthClick() {
gapi.auth.authorize(
{
client_id: CLIENT_ID,
scope: SCOPES.join(' '),
immediate: false
}, (authResult)=> {
this.handleAuthResult(authResult);
});
return false;
}
/**
* Calls an Apps Script function to list the folders in the user's
* root Drive folder.
*/
callScriptFunction(funcname, params, cb, devMode) {
console.log("callScriptFunction");
devMode = devMode || false;
// Create an execution request object.
var request = {
'function': funcname,
'parameters': params,
'devMode': devMode
};
// Make the API request.
var op = gapi.client.request({
'root': 'https://script.googleapis.com',
'path': 'v1/scripts/' + SCRIPT_ID + ':run',
'method': 'POST',
'body': request
});
op.execute(function(resp) {
if(resp.error && resp.error.status) {
// The API encountered a problem before the script
// started executing.
console.log('Error calling API:');
console.log(JSON.stringify(resp, null, 2));
} else if(resp.error) {
// The API executed, but the script returned an error.
// Extract the first (and only) set of error details.
// The values of this object are the script's 'errorMessage' and
// 'errorType', and an array of stack trace elements.
var error = resp.error.details[0];
console.log('Script error message: ' + error.errorMessage);
if(error.scriptStackTraceElements) {
// There may not be a stacktrace if the script didn't start
// executing.
console.log('Script error stacktrace:');
for(var i = 0; i < error.scriptStackTraceElements.length; i++) {
var trace = error.scriptStackTraceElements[i];
console.log('\t' + trace.function + ':' + trace.lineNumber);
}
}
} else {
// The structure of the result will depend upon what the Apps
// Script function returns. Here, the function returns an Apps
// Script Object with String keys and values, and so the result
// is treated as a JavaScript object (folderSet).
var result = resp.response.result;
//console.log("result:", result);
if(cb) {
cb(result);
}
}
});
}
}
import GasAuth from 'GasAuth';
import {COMPLETE, ERROR} from 'GasAuth';
const gasAuth = new GasAuth(callback);
function callback(type){
switch(type){
case "error":
break;
case "complete":
gasAuth.callScriptFunction("nanika", [], (res)=> {
console.log("result:", res);
}, false);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment