【问题标题】:Insufficient Permission error when setting Gmail delegate through service account通过服务帐户设置 Gmail 委托时出现权限不足错误
【发布时间】:2020-08-04 11:27:42
【问题描述】:

我有一个具有域范围委派的服务帐户,我正在尝试使用该帐户通过 App Script 应用程序设置 Gmail 委派。我正在为应用脚本使用 Google 的 OAuth2 库。

源用户(他们的邮件将被读取并被模拟执行操作)是普通的非管理员用户。运行以下代码时,我收到以下关于权限不足的错误:

{error={code=403.0, message=Insufficient Permission, errors=[{domain=global, reason=insufficientPermissions, message=Insufficient Permission}]}}

我可以很好地读取同一用户的委托,我只在尝试通过 API 修改其帐户上的任何属性时收到此错误。

我已尝试在“我的帐户”>“安全”部分中删除应用程序的权限并重新接受身份验证提示(尽管我在项目中启用了 Gmail 提示以确保安全,但我没有收到提示接受 Gmail 提示 - 它在清单中)。

我不确定问题出在哪里 - 有无数 StackOverflow 问题具有相同的错误代码,但似乎没有任何帮助 - 大多数人说要刷新凭据。

  • 使用服务帐户的项目启用了 Gmail API 和 Admin SDK
  • App Script 应用在管理控制台中设置为受信任。
  • 在服务帐户上启用了域范围的委派
  • DWD 服务帐户的客户端 ID 已在管理控制台中列入所有 Gmail 范围的白名单。
  • 我还尝试清除存储在脚本属性窗口中的授权。
  • 所有适用于 Gmail 的 Google API 都在 OAuth 同意屏幕页面上列入白名单

这是我的代码:

function setGmailDelegateTest() {

  var boxEmail = 'robert.terwilliger@x.com';
  var userEmail = 'ihyzy@x.com';
  var service = getGmailSharingService_(boxEmail);
  if (service.hasAccess()) {
    var url = 'https://www.googleapis.com/gmail/v1/users/' + boxEmail +'/settings/delegates';
    var response = UrlFetchApp.fetch(url, {
      method: 'post',
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      },
      body: {
        "delegateEmail": userEmail,
        "verificationStatus": "accepted"
      },
      muteHttpExceptions: true
    });
    var result = JSON.parse(response.getContentText());
    Logger.log(result);
  } else {
    Logger.log(service.getLastError());
  }
}
function getGmailSharingService_(boxEmail) { 
  return OAuth2.createService('gmail:' + boxEmail)
      .setTokenUrl('https://oauth2.googleapis.com/token')
      .setPrivateKey(PRIVATE_KEY)
      .setIssuer(CLIENT_EMAIL)
      .setSubject(boxEmail)
      .setPropertyStore(PropertiesService.getScriptProperties())
      .setScope('https://mail.google.com/ https://www.googleapis.com/auth/gmail.settings.sharing');
}

【问题讨论】:

    标签: google-apps-script oauth-2.0 gmail gmail-api google-workspace


    【解决方案1】:

    遇到这种情况是因为范围需要作为数组传递,因为有两个范围正在传递。请尝试将以下内容作为您的范围。

    .setScope(['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.settings.sharing']);
    

    最终分辨率

      var payload = {
          "delegateEmail": "jdoe@example.com",
          "verificationStatus": "accepted"
      }
    
      var options = {
      "method" : "POST",
      "contentType": "application/json",
      "muteHttpExceptions": true,
      "headers" : {
        "Authorization" : 'Bearer ' + service.getAccessToken()
      },
      "payload" : JSON.stringify(payload)
      };
    
      var url = 'https://www.googleapis.com/gmail/v1/users/' + user + 
      '/settings/delegates';
    
      var response = UrlFetchApp.fetch(url, options);
      var json = response.getContentText();
    

    【讨论】:

    • 天哪,我就知道是这样的——谢谢,解决了这个问题。现在它告诉我,我没有提供代表电子邮件,即使我可以看到我是:message=Missing delegate email, errors=[{domain=global, reason=invalidArgument 有什么想法吗?
    • 不幸的是,我遇到了同样的问题。这方面的文档不是最好的。如果我找到一个问题的解决方案,我一定会更新这篇文章。如果您碰巧找到了解决方案,请也这样做!
    • 请参考上面的“最终解决方案”
    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2015-11-28
    • 2021-01-14
    • 2020-11-25
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多