Home > other >  Calling an Apps Script API Executable function with parameters from another Apps script issues
Calling an Apps Script API Executable function with parameters from another Apps script issues

Time:10-16

I developed an Apps Script called "sudofunctions" which execute sensitive commands using an elevated account. It is shared with the entire domain and executes as the author.

I then developed "clientFunctions" which can be run by any authenticated user and needs to invoke funcntions in sudofunctions.

sudofunctions has 2 functions so far

function test()
{
  createUser("[email protected]", "Full name")

}
function createUser(email, name)
{
  console.log("Checkpoint Alpha")
}

clientFunctions then tries to call both these functions, calling test() works perfectly

  var token = ScriptApp.getOAuthToken();
  var options = {
    "method" : "POST", 
    "headers": {"Authorization": "Bearer "   token }, 
    "payload" : {
      "function": "test",
      "devMode": "true"
    },
    muteHttpExceptions:true
   }
  var rest = UrlFetchApp.fetch("https://script.googleapis.com/v1/scripts/ABCXYZ:run", options)
  

However, calling createUser fails

 var token = ScriptApp.getOAuthToken();
 var options = {
    "method" : "POST", 
    "headers": {"Authorization": "Bearer "   token }, 
    "payload" : {
      "function": "createUser",
      "parameters":["[email protected]", "John Doe"],
      "devMode": "true"
    },
    muteHttpExceptions:true
   }
  var rest = UrlFetchApp.fetch("https://script.googleapis.com/v1/scripts/ABCXYZ:run", options)
       

With the error:

    {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types."
          }
        ]
      }
    ]
  }
}

According to the documentation, it should work.

https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#request-body

Any ideas where I am going wrong?

Thanks for the help.

CodePudding user response:

In your script, from your error message, how about the following modification?

From:

 var options = {
    "method" : "POST", 
    "headers": {"Authorization": "Bearer "   token }, 
    "payload" : {
      "function": "createUser",
      "parameters":["[email protected]", "John Doe"],
      "devMode": "true"
    },
    muteHttpExceptions:true
   }

To:

var options = {
  "method": "POST",
  "headers": { "Authorization": "Bearer "   token },
  "contentType": "application/json",
  "payload": JSON.stringify({
    "function": "createUser",
    "parameters": ["[email protected]", "John Doe"],
    "devMode": "true"
  }),
  "muteHttpExceptions": true
}

Reference:

  • Related