I'm trying to use a Service Account to populate a spreadsheet for end users.
I am using this script to get the ChromeOS devices:
function listChromeOSDevices_(accessToken) {
var devices = [];
var pageToken;
do {
var params = {
method: 'get',
headers: {
Authorization: 'Bearer ' + accessToken
}
};
var url =
'https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/chromeos?projection=full&orderBy=status&sortOrder=ascending&maxResults=100';
if (pageToken) {
url += '&pageToken=' + pageToken;
}
var urlResponse = UrlFetchApp.fetch(url, params);
var jsonContent = JSON.parse(urlResponse.getContentText());
devices = devices.concat(jsonContent.chromeosdevices);
pageToken = jsonContent.nextPageToken;
} while (pageToken);
return devices;
}
Then trying to do this with the results:
function main() {
var userEmail = 'admin@email.com';
var accessToken = getOAuthToken(userEmail);
var chromeOSDevices = listChromeOSDevices_(accessToken);
var date = new Date().toLocaleDateString();
var outputTableHeaders = [
'Date and Duration of Usage',
'Asset ID',
'Location',
'User',
'Admin Notes',
'Firmware Version',
'Last Synchronised',
'MAC Address',
'Device Model',
'Serial Number',
'OS Version',
'Platform Version',
'Device Status',
'OU Path'
];
var deviceResults = [outputTableHeaders].concat(
chromeOSDevices.map(function(device) {
device = fillInMissing_(device);
device.activeTimeRanges = sortArrByDateElement_(
device.activeTimeRanges,
'asc'
);
return [
dateConv_(
device.activeTimeRanges[device.activeTimeRanges.length - 1].date
) +
' for ' +
msToReadable_(
device.activeTimeRanges[device.activeTimeRanges.length - 1]
.activeTime
),
device.annotatedAssetId,
device.annotatedLocation,
device.annotatedUser,
device.notes,
device.firmwareVersion,
device.lastSync,
device.macAddress,
device.model,
device.serialNumber,
device.osVersion,
device.platformVersion,
device.status,
device.orgUnitPath
];
})
);
}
However, I am getting the error message TypeError: Cannot call method "hasOwnProperty" of undefined.
The dateConv_ and msToReadable_ functions are helper functions - but they aren't the issue (have removed them and have the same problem).
When logging out chromeOSDevices in the main code, it returns the devices as expected (as far as I can tell).
However, when trying to use map it's almost as if the results have lost any additional properties. At a bit of a loss about this, as it looks fine to me!
I was previously using the same method to output the results of AdminDirectory.Chromeosdevices.list
(so I know that the map was working) but developed a need to use a service account for some of our users who do not have the required level of access to run the script, and that's where it has fallen over.
Anyone able to figure out why? Because I've been sitting looking at it for an hour or two and have nothing...
Edit
Removing the helper functions from the script I still get the error: TypeError: Cannot read property "annotatedAssetId" from undefined.
Edit 2
Logging it out like this:
for (var index in chromeOSDevices) {
Logger.log("INDEX: %s - The user is : %s",index,chromeOSDevices[index].annotatedUser)
}
I get this response: Response @ pastebin