Google apps script Gmail API调用返回空白,没有错误,但不应用更改来创建委托

Google apps script Gmail API调用返回空白,没有错误,但不应用更改来创建委托,google-apps-script,gmail,google-oauth,gmail-api,Google Apps Script,Gmail,Google Oauth,Gmail Api,我试图调用应用程序脚本中的Gmail API,同时模拟一个用户帐户,用于为其收件箱设置代理。以Google的示例为基础,我创建了以下函数来调用该服务并提供正确的信息,以便在收件箱中创建新的委托: function setGmailDelegate() { var formObject = { delegateReaderEmail: "homer.simpson@fox.com", delegateTargetEmail: "robert.terwilliger@fox.co

我试图调用应用程序脚本中的Gmail API,同时模拟一个用户帐户,用于为其收件箱设置代理。以Google的示例为基础,我创建了以下函数来调用该服务并提供正确的信息,以便在收件箱中创建新的委托:

function setGmailDelegate() {
  var formObject = {
    delegateReaderEmail: "homer.simpson@fox.com",
    delegateTargetEmail: "robert.terwilliger@fox.com"
  };

  var userEmail = formObject.delegateReaderEmail;
  var boxEmail = formObject.delegateTargetEmail;
  Logger.log("Letting "+userEmail + " read " + boxEmail);

  var service = getGmailAddDelegateService_(boxEmail);
  if (service.hasAccess()) {
    var url = 'https://www.googleapis.com/gmail/v1/users/' + boxEmail +'/settings/delegates';
    var response = UrlFetchApp.fetch(url, {
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      },
      body: {
        "delegateEmail": userEmail,
        "verificationStatus": "accepted"
      }
    });
    var result = JSON.parse(response.getContentText());
    Logger.log(result);
  } else {
    Logger.log(service.getLastError());
  }
}
此函数不返回任何内容-没有错误消息,但未创建委托。根据API,我应该得到一个支持,但我没有。我做错了什么

这是定义OAuth服务创建的函数:

function getGmailAddDelegateService_(boxEmail) { 
  return OAuth2.createService('Gmail:' + boxEmail)
      // Set the endpoint URL.
      .setTokenUrl('https://oauth2.googleapis.com/token')

      // Set the private key and issuer.
      .setPrivateKey(PRIVATE_KEY)
      .setIssuer(CLIENT_EMAIL)

      // Set the name of the user to impersonate.
      .setSubject(boxEmail)

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getScriptProperties())

      // Set the scope. This must match one of the scopes configured during the
      // setup of domain-wide delegation.
      .setScope(['https://mail.google.com/','https://www.googleapis.com/auth/gmail.settings.sharing']);
}

我试着改变了周围的范围,但没有用。我在G套件审计日志的执行日志中看不到任何指向任何问题的内容。

找到了答案:URLApp要求您指定您正在发布的数据:

var response = UrlFetchApp.fetch(url, {
      method: 'post',
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      },
      body: {
        "delegateEmail": userEmail,
        "verificationStatus": "accepted"
      }

我猜您没有得到任何输出,因为if service.hasAccess。这种情况可能是错误的。您是否获得第一个记录器的输出?关于Oauth部分,github示例是针对具有服务帐户的域范围的委托,这需要有一个付费的G套件帐户。这是你的情况吗?是的,我在一个付费的G套件帐户上,我已经设置了一个域范围的委派帐户,并用我需要的所有范围授权它。当我这里的范围出错时,我确实得到了输出,但现在我什么也没有得到。