0

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.

1 Answer 1

2

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.