Yassine Salah
Yassine Salah

Reputation: 1

How to assign Google Workspace licences using Appscript and a Service Account (SA)?

Is there a way to assign a Google Workspace Business Standard Licence to a user of a domain, using Appscript and a service account, but without having to use the rights of the user executing the script?

I am currently using an Appscript associated to an Appsheet app to create user's account and assigning them Google Workspace Business Standard licences. The creation part is done and works correctly but I am struggling with the licence assignment process.

I found this documentation for the licenseAssignments.listForProduct Method

And this list of Google Product and SKU IDs

When executing the request from the "Try it" panel of the Google Workspace website, everything works correctly and I get the expected output.

But I then tried the following code to use it with a SA:

function getLicences() 
{
  var productId = "Google-Apps"

  var serviceLicence = getService()
  if (serviceLicence.hasAccess()) 
  {
    var url = 'https://licensing.googleapis.com/apps/licensing/v1/product/'+productId+'/users';

    var options = 
    {
      headers: 
      {
        'Authorization': 'Bearer ' + serviceLicence.getAccessToken()
      },
      'method': 'get',
      'muteHttpExceptions': debug,
      'payload':
      {
        'customerId':'domain.com'
      }
      
    };
    var response = UrlFetchApp.fetch(url, options);
    if (response.getResponseCode() == 200) 
    {
      var group = JSON.parse(response.getContentText());
      Logger.log(group);


      return group;
    } 
    else 
    {
      Logger.log('Error fetching licences: ' + response.getContentText());
      return null;
    }
  } 
  else 
  {
    Logger.log('Service account does not have access: ' + serviceLicence.getLastError());
    return null;
  }
}

But it returned the following error:

    Error fetching licences: <!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/apps/licensing/v1/product/Google-Apps/users</code> was not found on this server.  <ins>That’s all we know.</ins>

I didn't manage to find the problem so your help would be really appreciated :)

Upvotes: 0

Views: 145

Answers (1)

Yassine Salah
Yassine Salah

Reputation: 1

I found the solution: the customerId must not be passed in the payload, however as path parameter in the URL

var url = "https://licensing.googleapis.com/apps/licensing/v1/product/"+ productId +"/users?customerId=domain.com" 

Upvotes: 0

Related Questions