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

112 views
Skip to first unread message

TempleSnr

unread,
Feb 1, 2024, 5:25:53 AM2/1/24
to Google Apps Script Community
Hi, I posted this in Stack Overflow but no answers, perhaps someone in the community might be able to advise please?

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)"

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?

thank you in advance


TempleSnr

unread,
Feb 2, 2024, 5:42:59 AM2/2/24
to Google Apps Script Community
for what it's worth, I was given delegated admin privileges for the scope that I need and the script works without needing to set up a project etc. Which isn't what I wanted, because the scripts will only run if the person running them has the privileges. My objective is for it to be one of a set of stand-alone scripts that can be used by users who have access to it, without them having extra Admin privileges.
So, if anyone is able to advise how I can get it working the preferred way, it would be much appreciated.
Thank you
Reply all
Reply to author
Forward
0 new messages