【问题标题】:Update Google Group Subscription with Google App Engine application使用 Google App Engine 应用程序更新 Google Group Subscription
【发布时间】:2020-07-26 05:02:58
【问题描述】:

我正在 Google App Engine(PHP) 上构建一个小应用程序,旨在将外部资源与 google 组同步,以获得最新的邮件列表。 (它应该作为 GAE cron 任务运行)

Google App Engine 项目和 google 组位于 GSuite 域内。

The sources are here,见 firestore2ggroup.php

我遇到的问题与安全相关,当我尝试调用 API 时,我收到 403 错误,没有其他详细信息。

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

我已按照此处描述的步骤作为起点,因为它接近我的需要:

https://medium.com/@Skaaptjop/access-gsuite-apis-on-your-domain-using-a-service-account-e2a8dbda287c

所以我做了以下事情:

  • 在谷歌云控制台中:

    • 部署 Google App Engine 项目
    • 转到 IAM 服务帐户,复制 GAE 服务帐户的唯一密钥并启用“域宽委派”
    • 创建 JSON 私钥
    • 使用“Service Directory Admin”(以及 Firestore 的 Cloud Data Store 用户)更新服务帐户
  • 在 GSuite 管理员中:

更新:我什至尝试添加更多权限,但仍然不起作用:

在我的项目中,我复制了 JSON 私钥,并在 app.yaml 中设置了变量 GOOGLE_APPLICATION_CREDENTIALS 与文件名。

我使用这个 Google lib 来访问 Google 组:

作曲家需要 google/apiclient:^2.0

try
{
  $client = new Google_Client();
  $client->setApplicationName('Pegass2GGroup');
  $client->setScopes(
    [
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP,
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER,
      Google_Service_Directory::ADMIN_DIRECTORY_USER
    ]);
  $client->useApplicationDefaultCredentials(true);
  $access_token = $client->fetchAccessTokenWithAssertion();

  print_r($access_token);

  $service = new Google_Service_Directory($client);

  /** @var $members  Google_Service_Directory_Members */
  $members = $service->members->listMembers($MAILING_LIST);

  /** @var $member  Google_Service_Directory_Member */
  foreach ($members as $member)
  {
    print_r($member->getEmail());
  }
  echo "</pre>";
}
catch(Exception $e)
{
  print_r($e);
  echo "</pre>";
}

我可以知道私钥已加载,因为 print_r($e) 给出了一个长异常并且列出了密钥。

print_r($access_token);给出以下内容:

Array
(
    [access_token] => dag2qssffdVpRZEr...0BsM_QUgQ
    [expires_in] => 3599
    [token_type] => Bearer
    [created] => 1586787451
)

$MAILING_LIST : 是邮件列表的完整电子邮件地址

【问题讨论】:

    标签: php google-app-engine google-workspace service-accounts


    【解决方案1】:

    所以上面的代码缺少模拟。

    $client->setSubject($EMAIL_TO_IMPERSONATE); //admin@mydomain.com

    在接触域中其他成员的数据时充当此电子邮件。

    然后我得到了这个错误:

    (
        [errors:protected] => 
        [message:protected] => {
      "error": "unauthorized_client",
      "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."
    }
    

    这是因为我在请求:

      Google_Service_Directory::ADMIN_DIRECTORY_GROUP,
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER,
      Google_Service_Directory::ADMIN_DIRECTORY_USER
    

    但我忘记在 GSuite API 安全管理中添加以下内容:

      Google_Service_Directory::ADMIN_DIRECTORY_USER
    

    添加后,它起作用了。 因此,如果您请求一组角色,则必须全部授予。 即使我没有使用 Google_Service_Directory::ADMIN_DIRECTORY_USER,我在连接时询问它并且其中一个未被授予的事实也会阻止整个连接。

    我想我只需要 Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER

    到目前为止,我仅列出组成员的工作范围是:

    184920....0484899(客户端 ID)

    https://www.googleapis.com/auth/admin.directory.group,https://www.googleapis.com/auth/admin.directory.group.member,https://www.googleapis.com/auth/apps.groups.settings,https://www.googleapis.com/auth/groups,https://www.googleapis.com/auth/admin.directory.user

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 2010-12-23
      • 2013-02-05
      • 2011-03-08
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多