wjwelch1
wjwelch1

Reputation: 79

Calling Google Apps Script in another Project File

I am trying to call a Google Apps Script file that is in another project file following the sample here using UrlFetchApp.fetch.
I'm getting the same error that the original poster mentions but I am not having an success with my sample.
Did Google change something in the last 4 years that prevents me from calling the other script file?
See script below.

Below is the function that I am using to call the other project file

    function makeRequest() 
    {
      var webAppUrl = "https://script.google.com/macros/s/***/exec";

      var auth = ScriptApp.getOAuthToken();
      var header = { 'Authorization': 'Bearer ' +  auth };
      var options = { 'method':'post', 'headers':header };

      var resp = UrlFetchApp.fetch(webAppUrl, options);

      Logger.log(resp);
    }

Below is the function that I am trying to call. Additionally, I have ran the authorizeDrive function and published as a webapp.

    function authorizeDrive()
    {
      var forScope = DriveApp.getRootFolder();
    }

    function doPost()
    {  
      var ss = SpreadsheetApp.openById('ssID');
      var name = ss.getName();
      Logger.log('called');
      return ContentService.createTextOutput(name);
    }

Upvotes: 0

Views: 306

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to run the Google Apps Script in the GAS project A by accessing to Web Apps from the GAS project B.
  • In your case, Web Apps is deployed by Who has access to the app: of Only myself or Anyone.
    • You want to access to Web Apps using the access token.
  • The GAS project A and B are in your Google Drive.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

I think that in your case, the scope is required to be added to the project including makeRequest(). So in order to add the scope for accessing to Web Apps using the access token, how about the following modification?

Modified script:

function makeRequest() 
{
  var webAppUrl = "https://script.google.com/macros/s/***/exec";

  var auth = ScriptApp.getOAuthToken();
  var header = { 'Authorization': 'Bearer ' +  auth };
  var options = { 'method':'post', 'headers':header };

  var resp = UrlFetchApp.fetch(webAppUrl, options);

  Logger.log(resp);
}

// DriveApp.getFiles() // This comment line is used for automatically detecting the scope.
  • Please add the // DriveApp.getFiles() of the comment line. This comment line is used for automatically detecting the scope.
  • In this case, https://www.googleapis.com/auth/drive.readonly is added to the scopes. If this didn't resolve your issue, please add the comment line of // DriveApp.createFile(blob). In this case, https://www.googleapis.com/auth/drive is added.

Note:

  • When the script of Web Apps side is modified, please redeploy it as new version. By this, the latest script is reflected to Web Apps. Please be careful this.
  • If the owner of GAS project of Web Apps is not your account which has the script of makeRequest(), at first, please share the GAS project file of Web Apps with your account. Then, please test it. This specification has added at April 11, 2018. Also, please be careful this.

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 2

Related Questions