Google Admin SDK - 服务帐户无法访问资源

Google Admin SDK - Service account can't access resource

我已经通过 console.developers.google.com 创建了服务帐户客户端。生成的密钥、.p12 证书、启用的 DwD 等等...... 然后我继续在管理员 API references. (HTTP/REST version). That included asingning scopes to these clients id at Google Admin -> Security -> Advancet Settings. Current admin security config state 中作为描述者 在我成功交换访问令牌代码并请求我的域下的用户列表后,我收到了此响应。

{   
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "forbidden",
        "message": "Not Authorized to access this resource/api"
      }
    ],
    "code": 403,
    "message": "Not Authorized to access this resource/api" 
  } 
}

请求是 GET,授权承载 ya29.ElrxA8F[acctoken 的其余部分] 并且 google 开发控制台仪表板显示带有 Admin SDK 错误请求的图表

你可以参考这个answer which states that only super admins, delegated admins and resellers can access the Admin SDK Directory API. Try to delegate an admin that only has access to update users via the API and then having your web application utilize an OAuth token created for this delegated admin. This might also because of misconfiguration on Google App "Add the Client ID with scopes on the Google Apps console.". You may check on this

您必须将您的客户冒充为指定用户

所以我错过了:json 身份验证负载中的 "sub" 参数: 这是正确的 json 对象(当然还有您的 ID 等),您需要使用它从服务帐户访问您的 Admin SDK:

{
"iss":"761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com", "sub":"some.user@example.com",
"scope":"googleapis.com/auth/prediction",
"aud":"googleapis.com/oauth2/v4/token",
"exp":1328554385, "iat":1328550785 }

查看 google admin sdk reference 了解更多详情。 (带有 "sub" 参数的部分)

对于以后也有这个问题的人。请也重置您的令牌...我花了一天时间不明白为什么我的 Google 脚本中使用我的服务帐户会出现此错误,即使我已经验证了所有权限。最后,原因是脚本的缓存使用了在缺少权限的情况下生成的旧令牌。