Search code examples
google-apps-scriptgoogle-admin-sdkgoogle-reporting-api

Retrieve Mobile Device Report via Google Reports API via google scripts


I am trying to retrieve the mobile device report but I keep getting this error: "API call to directory.mobiledevices.list failed with error: Bad Request".

I think the issue is the customerId. What should I use to retrieve all mobile devices info? I have tried using "all" and also defining the domain name but no luck.

Must appreciated for your time and input! TIA!

function mobileReport() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('SHEETNAME');
  sheet.clear();

  // Append the headers.
  var headers = ['Full Name','Email','Device Id','Model','Type','Status','Last Sync'];
  sheet.appendRow(headers).setFrozenRows(1);

  var rows = [];
  var pageToken;
  var count = 0;

  do {
    var page = AdminDirectory.Mobiledevices.list({
     orderBy: 'email',
     maxResults: 500,
     pageToken: pageToken    
    });
    var users = page.mobiledevices;
    if (users) {
      for (var i = 0; i < users.length; i++) {
        var user = users[i];           
        rows.push([user.name,user.email,user.deviceId,user.model,user.type,user.status,user.lastSync]);
      }
      // Append the results.
      sheet.getRange(2, 1, rows.length,headers.length).setValues(rows);
    } 
    else {
    Logger.log('No users found.');
  } 
    pageToken = page.nextPageToken;
  } while (pageToken);
}

Solution

  • Modification points:

    • When I saw the official document, it seems that at the method of "Method: mobiledevices.list" in Directory API, customerId is required to be used as follows.

      customerId: The unique ID for the customer's Google Workspace account. As an account administrator, you can also use the my_customer alias to represent your account's customerId. The customerId is also returned as part of the Users resource.

      • But in your script, this is not used. By this, the arguments for AdminDirectory.Mobiledevices.list are not correct. I think that this is the reason of your issue of "Bad Request".
    • The maximum value of maxResults is 100.

    • It seems that the email of Enum of orderBy is EMAIL. Ref

    • In your script, setValues is used in the loop and the same range is used. In this case, the values are overwritten by every loop.

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    Please modify your script as follows.

    From:
    var rows = [];
    var pageToken;
    var count = 0;
    
    do {
      var page = AdminDirectory.Mobiledevices.list({
       orderBy: 'email',
       maxResults: 500,
       pageToken: pageToken    
      });
      var users = page.mobiledevices;
      if (users) {
        for (var i = 0; i < users.length; i++) {
          var user = users[i];           
          rows.push([user.name,user.email,user.deviceId,user.model,user.type,user.status,user.lastSync]);
        }
        // Append the results.
        sheet.getRange(2, 1, rows.length,headers.length).setValues(rows);
      } 
      else {
      Logger.log('No users found.');
    } 
      pageToken = page.nextPageToken;
    } while (pageToken);
    
    To:
    var customerId = "###"; // <--- Please set customerId.
    
    var rows = [];
    var pageToken;
    var count = 0;  // It seems that this variable is not used in the script.
    do {
      var page = AdminDirectory.Mobiledevices.list(customerId, {
        orderBy: 'EMAIL',
        maxResults: 100,
        pageToken: pageToken
      });
      var users = page.mobiledevices;
      if (users.length > 0) {
        for (var i = 0; i < users.length; i++) {
          var user = users[i];
          rows.push([user.name, user.email, user.deviceId, user.model, user.type, user.status, user.lastSync]);
        }
      } else {
        Logger.log('No users found.');
      }
      pageToken = page.nextPageToken;
    } while (pageToken);
    if (rows.length > 0) {
      sheet.getRange(2, 1, rows.length, headers.length).setValues(rows);
    }
    

    Note:

    • In this modified script, it supposes that you can retrieve the values using AdminDirectory.Mobiledevices.list. Please be careful this.

    Reference: