使用应用程序脚本API创建新的应用程序脚本文件-错误-接收到无效的JSON负载

使用应用程序脚本API创建新的应用程序脚本文件-错误-接收到无效的JSON负载,json,google-apps-script,oauth-2.0,google-apps-script-api,Json,Google Apps Script,Oauth 2.0,Google Apps Script Api,我得到了一个错误: 接收到无效的JSON负载 尝试使用应用程序脚本API创建新的应用程序脚本文件时 如何修复该错误 function createNewFile() { var d,options,payload,response,theAccessTkn,url; theAccessTkn = ScriptApp.getOAuthToken(); //See https://developers.google.com/apps-script/api/reference/rest

我得到了一个错误:

接收到无效的JSON负载

尝试使用应用程序脚本API创建新的应用程序脚本文件时

如何修复该错误

function createNewFile() {
  var d,options,payload,response,theAccessTkn,url;

  theAccessTkn = ScriptApp.getOAuthToken();

  //See https://developers.google.com/apps-script/api/reference/rest/v1/projects/create
  url = "https://script.googleapis.com/v1/projects";

  d = new Date().toString().slice(0,10);

  payload = {
    "title": "AA_" + d
  }

  options = {
    "method" : "POST",
    "muteHttpExceptions": true,
    "headers": {
       'Authorization': 'Bearer ' +  theAccessTkn
     },
    "payload": JSON.stringify(payload)
  };

  response = UrlFetchApp.fetch(url,options);

  Logger.log(response)

  return response;
}
我在清单文件中设置了授权范围,以避免需要添加OAuth库:

{
  "timeZone": "America/New_York",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.projects",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER"
}
我需要在选项中添加
“contentType”:“application/json”

options = {
  "method" : "POST",
  "muteHttpExceptions": true,
  "headers": {
     'Authorization': 'Bearer ' +  theAccessTkn
   },
  "contentType": "application/json",
  "payload": JSON.stringify(payload)
};

您是否尝试过在
标题中指定
内容类型:“application/json;charset=UTF-8”
。默认为
application/x-www-form-urlencoded
否,我将尝试添加
contentType
。谢谢。我添加了
“contentType”:“application/json”
,现在它可以工作了。感谢您发布您的解决方案。。。我一时忘记了
JSON.stringify
payload。