Yes this is possible. Previously you would use the OAuth2 library, save your credentials to the script property store, then use the REST interface to the service you want to access with your own credentials. Now you can use the Execution API to use the built in services.
Setup
Add the OAuth2 Library to your project:
https://github.com/gsuitedevs/apps-script-oauth2
Set it up according the the README.md, matching the scopes of your project(look at the project properties). Make sure your point setPropertyStore() to the script properties.
Authenticate your OAuth2 service.
Publish your script as an Execution API
Add a call to your scripts REST interface. Look at the token method, make sure you change it to match your own.
function ExecuteAsMe(functionName,paramsArray){
var url = 'https://script.googleapis.com/v1/scripts/'+ScriptApp.getProjectKey()+':run';
var payload = JSON.stringify({"function": functionName ,"parameters":paramsArray, "devMode": true});
var params={method:"POST",
headers:{Authorization: "Bearer "+ getFusionService().getAccessToken()},
payload:payload,
contentType:"application/json",
muteHttpExceptions:true};
var results = UrlFetchApp.fetch(url, params);
return JSON.parse(results).response.result;
}
- Execute the function as you:
var results = ExecuteAsMe("searchFusionTable",['searchTerm', 50]);
Reference:
Execution API
https://developers.google.com/apps-script/guides/rest/api?hl=en
OAuth2 Library
https://github.com/gsuitedevs/apps-script-oauth2