云端构建API部署:无法在Google Apps脚本上运行

时间:2019-08-14 10:04:28

标签: google-apps-script google-cloud-platform google-cloud-build

我确认了GAS服务帐户的授权。 “列表”是有效的,但是“运行”方法永远无效。错误消息是“源不得为空”。我应该附上哪种json?

这是在使用GSApp库的独立GAS上。 (Apps-Script-GSApp-Library:MJ5317VIFJyKpi9HCkXOfS0MLm9v2IJHf)

function deploy() {
  var jsonKey = JSON.parse(PropertiesService.getScriptProperties().getProperty("jsonKey"));
  var serverToken = new GSApp.init(jsonKey.private_key, ["https://www.googleapis.com/auth/cloud-platform"], jsonKey.client_email);
  var tokens = serverToken.addUser(jsonKey.client_email).requestToken().getTokens();
  var url = "https://cloudbuild.googleapis.com/v1/projects/{ProjectId}/triggers/{TriggerId}:run";
  var options = {
    "muteHttpExceptions": true,
    "method": "POST",
    "headers": {
      "Authorization":"Bearer "+tokens[jsonKey.client_email].token,
    },
    "source": {
      "projectId": "{ProjectId}",
      "branchName": "master",
      "repoName": "repo"
      }
  }
  Logger.log(UrlFetchApp.fetch(url,options));
}
{
  "error": {
    "code": 400,
    "message": "source must not be empty",
    "status": "INVALID_ARGUMENT"
  }
}

1 个答案:

答案 0 :(得分:0)

UrlFetchApp.fetch()无法将“ 来源”识别为有效属性。使用“ 有效载荷”代替。另外,您还需要JSON.stringify()的有效负载并将contentType属性设置为application/json,如下所示:

var options = {
    "muteHttpExceptions": true,
    "method": "POST",
    "contentType":"application/json",
    "headers": {
        "Authorization":"Bearer "+tokens[jsonKey.client_email].token,
    },
    "payload": JSON.stringify({
        "projectId": "{ProjectId}",
        "branchName": "master",
        "repoName": "repo"
    })

};