Ken'ichi Ogawa
Ken'ichi Ogawa

Reputation: 43

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

On Google Admin screen, I can get numbers of available licenses and used licenses shown below:

Google Admin Subscription

How can I get these numbers via API?

Note: I read this question and tried, but not worked well.

-- EDIT: 2021/07/15 --

My request:

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

Response from API:

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

Expectation: I want to get the data same as 2 available, 1132 assigned as the GUI shows.

To be honestly, I'm not satisfying even if I can get info via this API, because it seems not responding real-time data like GUI.

Upvotes: 1

Views: 4377

Answers (3)

Mujeeb
Mujeeb

Reputation: 1215

I was not able to use Reseller API (ran into some authorization issues) and Reports API (contained null values in all relevant attributes). The only way I was able to find how many licenses were remaining was through Enterprise License Manager API.

After getting assignments, I used sdkName to filter records based on the type of license.

Here is the complete code.

function getRemainingGoogleUserLicensesCount() {
    const GOOGLE_USER_LICENSES_TOTAL = 100 // You can find this from Google Admin -> Billing -> Subscriptions 
    const productId = 'Google-Apps';
    const customerId = 'C03az79cb'; // You can find this from this response, https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#json-response
    let assignments = [];
    let usedLicenseCount = 0
    let pageToken = null;
    do {
      const response = AdminLicenseManager.LicenseAssignments.listForProduct(productId, customerId, {
        maxResults: 500,
        pageToken: pageToken
      });
      assignments = assignments.concat(response.items);
      pageToken = response.nextPageToken;
    } while (pageToken);
  
    for (const assignment of assignments) {
      if (assignment["skuName"] == "Google Workspace Business Plus") {
        usedLicenseCount += 1
      }
    }
    return GOOGLE_USER_LICENSES_TOTAL - usedLicenseCount
}

Upvotes: 0

Rafa Guillermo
Rafa Guillermo

Reputation: 15357

Answer:

You can only get the number of assigned licenses using the API, the number available isn't exposed and so does not get returned.

More Information:

Given that you have licenses assigned for your domain, and the user that is querying the API has access to this information, you can retrieve the data with the following request:

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

While not necessary, I added the parameter field=* in order to make sure all data is returned.

This gave me a response as such:

{
  "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"
        }
      ]
    }
  ]
}

Here you can see that the intValue for accounts:gsuite_unlimited_total_licenses is 233 - which is reflected in the UI: enter image description here

Feature Request:

You can however let Google know that this is a feature that is important for access to their APIs, and that you would like to request they implement it.

Google's 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, with the Feature Request template.

Upvotes: 1

Vasil Nikolov
Vasil Nikolov

Reputation: 757

I think there are 2 ways this information can be obtain, but I can confirm for only one of them.

1. Using the Report API that you mentioned.

  • NOTE : The report is not live data, so you must run the API call with a "date" parameter set at least 2 days before the execution date
  • Given that, you would have to run this GET method with the proper date in the {date} param
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
    }
]

Important : The repot will always date 2 day back, so you can get the total number of licenses gsuite_enterprise_total_licenses in my example, and then use the Enterprise License Manager API to retrieve all currently assigned licenses

2. Using the Reseller API

  • Retrieving the information from the reseller point of view you would need to use the subscriptions.get method, providing your customerId and subscriptionId , calling the following GET request:
GET https://reseller.googleapis.com/apps/reseller/v1/customers/{customerId}/subscriptions/{subscriptionId}
  • The response of that would be a subscriptions resource, that contains various information about the license and the Seats object , which if you expand looks like this :
{
  "numberOfSeats": integer,
  "maximumNumberOfSeats": integer,
  "licensedNumberOfSeats": integer,
  "kind": string
}

Upvotes: 3

Related Questions