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 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:

    Reference: