Running Apps Script in a spreadsheet, trying to get AdminLicenseManager to work

42 Views Asked by At

I have a collection of apps script functions to get user information into a spreadsheet which work fine. Now I'm trying to add a column with the user's license type. My understanding is that I need to use "Enterprise License Manager API service settings", which I have added but I was getting errors "Error retrieving license for user@domain: Cannot read properties of undefined (reading 'LicenseAssignments')". I am not a superadmin, but I do have delegated privileges. I've been through the various documents on this and my understanding is that I had to do the following:

  1. create a CGP project - done
  2. change my apps script's project from standard GCP to my new GCP project
  3. enable Admin SDK API - done, then I created credentials successfully, I tried to run the script but then it told me I needed:
  4. enable Enterprise License Manager API - done

I run the standard script downloaded from Google's github apps script samples and I get an error "GoogleJsonResponseException: API call to licensing.licenseAssignments.listForProduct failed with error: Unauthorized operation for the given domain. at getLicenseAssignments(getLicenses:7:61)"

/**
 * Logs the license assignments, including the product ID and the sku ID, for
 * the users in the domain. Notice the use of page tokens to access the full
 * list of results.
 */
function getLicenseAssignments() {
  const productId = 'Google-Apps';
  const customerId = 'example.com';
  let assignments = [];
  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);
  // Print the productId and skuId
  for (const assignment of assignments) {
    console.log('userId: %s, productId: %s, skuId: %s',
        assignment.userId, assignment.productId, assignment.skuId);
  }
}

As someone who's quite new to this environment, have I done the right things? What have I missed?

0

There are 0 best solutions below