google-apps-scripthttp-status-code-500google-apps-script-api

Why does running Apps Script function from Apps Script API returns 500?


Keep in mind the difference between Apps Script(AS for short) projects (code.gs) and the Apps Script API.

The general idea is to be able to create AS projects, write functions inside and execute them, all from a 'parent' AS project.

After confirming that there was no AS-specific method for creating new AS projects (other than by hand), I recently managed to get the AS API working from within an AS project.

The projects.create method works great for creating new projects.

The projects.updateContent method works great for writing code inside newly created projects.

However, I'm having trouble using the script.run method to try and execute functions contained in new projects from the API

Here is how I tried to get it working:

function runScript() {

    var scriptId = 'target_script_id'
    var functionName = 'target_function_name';

    var accessToken = ScriptApp.getOAuthToken();

    var url = 'https://script.googleapis.com/v1/scripts/' + scriptId + ':run';

    var payload = {
        'function': functionName,
        'devMode': false
    };
  
    var options = {
        'method': 'post',
        'contentType': 'application/json',
        'payload': JSON.stringify(payload),
        'muteHttpExceptions': true,
        'headers': {
            'Authorization': 'Bearer ' + accessToken
         }
    };
  
    var response = UrlFetchApp.fetch(url, options);
    var responseData = JSON.parse(response.getContentText());
    Logger.log (responseData);
    return responseData; 
}  

and here is the error I received:

{
  "error": {
    "code": 500,
    "message": "Internal error encountered.",
    "status": "INTERNAL"
  }
}

Solution

  • After further investigating in the AS-API documentation, it turns out that the target script first needs to be deployed as API-executable. Then I set the deployment id as target in the script.run request, and the targeted function is executed.

    However, it only postponed my problem, because now I can't deploy the target script from the API using the deployments.create method, I get a 500 error again.