-->

Mobiledevices: action missing

2020-04-01 06:56发布

问题:

I am trying to perform a POST of the action, however, when I make the request I get code 400 speaking that the action value is missing.

my code:

function mobileAPIPOST() {

  var response = UrlFetchApp.fetch(
    "https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/mobile/fakexQ_fake0Z0dKFdSblfakeTnnfakel7IYwixfake4ddZfakeAIvnmu/action", {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
        "Content-Type": "application/json",
      },
      data: {
        "action": "block"
      },
      muteHttpExceptions: true
    });

}

I updated the code following the response from @TheMaster.

My updated code:

function mobileAPIPOST() {

  var response = UrlFetchApp.fetch(
    "https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/mobile/fakexQ_fake0Z0dKFdSblfakeTnnfakel7IYwixfake4ddZfakeAIvnmu/action", {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
        "Content-Type": "application/json",
      },
      payload: {
        "action": "block"
      },
      muteHttpExceptions: true
    });

}

error pointed out:

{
  "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "parseError",
        "message": "Parse Error"
       }
      ],
      "code": 400,
      "message": "Parse Error"
     }
    }

回答1:

UrlFetchApp doesn't have a data key parameter. Use payload instead.

Reference:

UrlFetchApp

Code Snippet:

UrlFetchApp.fetch(url,
    {
      method: "POST",
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken(),
      },
      contentType:  'application/json',
      payload: JSON.stringify({
        "action": "block"
      }),
      muteHttpExceptions: true
    });