【问题标题】:GMail API set non-primary send as signature return error 403GMail API 设置非主要发送作为签名返回错误 403
【发布时间】:2021-05-24 18:51:11
【问题描述】:

我正在制作一个应该为域中的每个用户设置签名的应用程序。当我尝试在主别名上设置签名时,这工作正常,但此解决方案不适用于其他别名(非主别名)。

我使用了一个运行良好的域范围委派,因为我可以将所有主要发送的签名设置为域中的别名。为此,我使用请求:'www.googleapis.com/gmail/v1/users//settings/sendAs/'。当我对非主要别名执行完全相同的操作时,我收到一条错误 403 并显示一条消息,告诉我缺少范围“www.googleapis.com/auth/gmail.settings.sharing”。

Missing required scope "https://www.googleapis.com/auth/gmail.settings.sharing" for modifying non-primary SendAs

这些是我在代码中使用的范围:

"oauthScopes": [
    "https://www.googleapis.com/auth/gmail.settings.basic",
    "https://www.googleapis.com/auth/gmail.settings.sharing",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/admin.directory.user.readonly",
    "https://www.googleapis.com/auth/drive.readonly"
  ]

如您所见,存在“共享”范围。

// The service that allow me to list send as alias
var serviceListe = getDomainWideDelegationService('Gmail: ', 'https://www.googleapis.com/auth/gmail.settings.basic', user.primaryEmail)

// THe service that allow me to edit send as signature
var serviceModif = getDomainWideDelegationService('Gmail: ', 'https://www.googleapis.com/auth/gmail.settings.sharing', user.primaryEmail)

返回域范围委托的代码:

function getDomainWideDelegationService(serviceName, scope, email) {
  return OAuth2.createService(serviceName + email)
      // Set the endpoint URL.
      .setTokenUrl('https://oauth2.googleapis.com/token')

      // Set the private key and issuer.
      .setPrivateKey(OAUTH2_SERVICE_ACCOUNT_PRIVATE_KEY)
      .setIssuer(OAUTH2_SERVICE_ACCOUNT_CLIENT_EMAIL)

      // Set the name of the user to impersonate. This will only work for
      // Google Apps for Work/EDU accounts whose admin has setup domain-wide
      // delegation:
      // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
      .setSubject(email)

      // 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(scope);
}

【问题讨论】:

  • scope 变量长什么样?我假设您在清单中定义了范围?
  • 范围是在清单中手动定义的,因为它不会自动添加“共享”。如果你在getDomainWideDelegationService 中询问scope,它要么是'googleapis.com/auth/gmail.settings.basic',要么是'googleapis.com/auth/gmail.settings.sharing'。我还尝试在以逗号分隔的单个调用中添加两者,但这并没有解决问题。
  • 根据文档here,“scope 字段指定了以空格分隔的访问范围列表”。因此,我建议您尝试相应地定义 scope 变量。您仍然收到错误消息吗?
  • 好吧,我现在感觉有点傻……解决方案实际上是用空格分隔作用域。当我设置域范围的委派时,提示要求我用逗号分隔范围,所以我认为这在代码中是相同的 --'。

标签: google-apps-script gmail-api administrator google-gsuite


【解决方案1】:

根据文档here:

scope - 此字段指定一个以空格分隔的访问范围列表,这些访问范围对应于您的应用程序可以代表用户访问的资源。这些值通知 Google 向用户显示的同意屏幕。

考虑到这一点,我建议您使用 空格 和 not 逗号 分隔范围。

参考

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 2019-01-28
    • 2016-11-10
    • 1970-01-01
    • 2017-05-26
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多