John Smith
John Smith

Reputation: 397

Google Apps Script Execution API - Calling app is Apps Script - Apps Script to Apps Script

I have two scripts in different domains, I would like to fetch data from one to the other.

I've found Script Execution API to be useful, however they don't provide any example GAS to GAS. https://developers.google.com/apps-script/guides/rest/quickstart/target-script

Any suggestion/guidance on how to accomplish such thing?

Upvotes: 1

Views: 2459

Answers (2)

SIDMISH
SIDMISH

Reputation: 166

This answer might be helpful. what i have done is that created a cloud platform project and created two standalone project, one contains the calling script and other has the method you want to call and must be deployed as a API Executable.

IMPORTANT: Both these project must be associated with the Cloud Platform Project.

For associating the projects to Cloud Platform's project , in script editor do this, Resources --> Cloud Platform Project. You'll see the dialog box which has input box having placeholder Enter Project Number Here, here put the Cloud Platform's project number and do the same for other project as well.

Now as i have followed google's quick start tutorial for EXECUTION API. i have a target script (Script containing your method) as follows:

/**
 * The function in this script will be called by the Apps Script Execution API.
 */

/**
 * Return the set of folder names contained in the user's root folder as an
 * object (with folder IDs as keys).
 * @return {Object} A set of folder names keyed by folder ID.
 */
function getFoldersUnderRoot() {
  var root = DriveApp.getRootFolder();
  var folders = root.getFolders();
  var folderSet = {};
  while (folders.hasNext()) {
    var folder = folders.next();
    folderSet[folder.getId()] = folder.getName();
  }
  return folderSet;
}

NOTE: This script must be deployed as a API Executable. In project you must have enabled the Google Execution API.

Now the calling script,

function makeRequest(){
  // DriveApp.getRootFolder();
  var access_token=ScriptApp.getOAuthToken();
  var url = "https://script.googleapis.com/v1/scripts/{YourScriptId}:run";
  var headers = {
    "Authorization": "Bearer "+access_token,
    "Content-Type": "application/json"
  };
  var payload = {
    'function': 'getFoldersUnderRoot',
    devMode: true
  };
  var res = UrlFetchApp.fetch(url, {
    method: "post",
    headers: headers,
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  });
  Logger.log(res);  
}

On first line inside function you can see i have commented out the Drive API call. I did that because the calling script and target script must have the same scope.

Upvotes: 2

abielita
abielita

Reputation: 13469

Yes you can use Execution API to fetch data from one domain to other. From the documentation, it consists of a single scripts resource, which has a single method, run, that makes calls to specific Apps Script functions.

The run method must be given the following information when called:

Here is an example from GitHub:

var script = google.script('v1');
var key = require('./creds.json');
var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/spreadsheets'], null);

jwtClient.authorize(function(err, tokens) {
  if (err) {
    console.log(err);
    return;
  }
  script.scripts.run({
    auth: jwtClient,
    scriptId: 'script-id',
    resource: {
      function: 'test',
      parameters: []
    }
  }, function(err, response) {
    if (err) {
      throw err;
    }
    console.log('response', response);
  })

});

You can also check this related SO question on how to use Google Apps Script Execution API and Javascript to make the interaction between website and Google Drive.

Upvotes: 1

Related Questions