如何通过 Google Workspace Admin SDK 获得可用许可证?

How can I get available license via Google Workspace Admin SDK?

在 Google 管理屏幕上,我可以获得如下所示的可用许可证和已用许可证的数量:

如何通过 API 获取这些数字?

注意:我阅读 this question 并尝试过,但效果不佳。

-- 编辑:2021/07/15--

我的要求:

https://developers.google.com/admin-sdk/reports/reference/rest/v1/customerUsageReports/get

来自API的回复:

{
  "kind": "admin#reports#usageReports",
  "etag": "\"occ7bTD-Q2yefKPIae3LMOtCT9xQVZYBzlAbHU5b86Q/gt9BLwRjoWowpJCRESV3vBMjYMc\""
}

期望:我希望获得与 GUI 显示的 2 available, 1132 assigned 相同的数据。

老实说,即使我可以通过这个 API 获取信息,我也不满意,因为它似乎没有像 GUI 那样响应 real-time 数据。

答案:

您只能使用 API 获取分配的许可证数量,可用数量不会公开,因此不会返回。

更多信息:

鉴于您已为您的域分配了许可证,并且查询 API 的用户有权访问此信息,您可以使用以下请求检索数据:

curl \
  'https://admin.googleapis.com/admin/reports/v1/usage/dates/2021-07-10?parameters=accounts%3Agsuite_unlimited_total_licenses&fields=*&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

虽然没有必要,但我添加了参数field=*以确保返回所有数据。

这给了我这样的回应:

{
  "kind": "admin#reports#usageReports",
  "etag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "usageReports": [
    {
      "kind": "admin#reports#usageReport",
      "date": "2021-07-10",
      "etag": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "entity": {
        "type": "CUSTOMER",
        "customerId": "C0136mgul"
      },
      "parameters": [
        {
          "name": "accounts:gsuite_unlimited_total_licenses",
          "intValue": "233"
        }
      ]
    }
  ]
}

在这里您可以看到 accounts:gsuite_unlimited_total_licensesintValue 是 233 - 这反映在 UI 中:

功能请求:

但是,您可以让 Google 知道这是一项对于访问他们的 API 很重要的功能,并且您希望请求他们实现它。

Google 的 Issue Tracker is a place for developers to report issues and make feature requests for their development services, I'd urge you to make a feature request there. The best component to file this under would be the Google Admin SDK component,使用 Feature Request 模板。

我认为可以通过两种方式获取此信息,但我只能确认其中一种方式。

1. 使用您提到的 Report API

  • 注意:该报告不是实时数据,因此您必须 运行 API 调用至少 2 天的“日期”参数执行日期前
  • 鉴于此,您必须 运行 此 GET 方法在 {date} 参数
  • 中使用正确的日期
GET https://admin.googleapis.com/admin/reports/v1/usage/dates/{date}
[
    {
        "BoolValue":  null,
        "DatetimeValueRaw":  null,
        "DatetimeValue":  null,
        "IntValue":  12065,
        "MsgValue":  null,
        "Name":  "accounts:gsuite_enterprise_total_licenses",
        "StringValue":  null
    },
    {
        "BoolValue":  null,
        "DatetimeValueRaw":  null,
        "DatetimeValue":  null,
        "IntValue":  12030,
        "MsgValue":  null,
        "Name":  "accounts:gsuite_enterprise_used_licenses",
        "StringValue":  null
    }
]

重要 :repot 将始终追溯到 2 天前,因此您可以在我的示例中获得许可证总数 gsuite_enterprise_total_licenses,然后使用 Enterprise License Manager API 检索所有当前分配的许可证

2. 使用 Reseller API

  • 从经销商的角度检索信息,您需要使用 subscriptions.get 方法,提供您的 customerIdsubscriptionId ,调用以下 GET 请求:
GET https://reseller.googleapis.com/apps/reseller/v1/customers/{customerId}/subscriptions/{subscriptionId}
  • 它的响应将是一个 subscriptions 资源,其中包含有关许可证和 Seats object 的各种信息,如果您展开它,它看起来像这样:
{
  "numberOfSeats": integer,
  "maximumNumberOfSeats": integer,
  "licensedNumberOfSeats": integer,
  "kind": string
}