Im trying to get a response from the Google Cloud Identity API and fill out a Google sheet with specific results such as policy name, setting name and values from the settings. The code is below and is working as I want it too. However im only getting 50 results from the API and I should be getting 228 results, which I believe is something to do with getting the next page of results. I've done some research online, but cant figure out how to get it to return more then 50 results. Any help appreciated!
function get_settings() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName("07. settings api");
const api_key = PropertiesService.getScriptProperties().getProperty('apikey');
var url = "https://cloudidentity.googleapis.com/v1beta1/policies?key=" + api_key;
const params = {
headers: {
"method": "get",
"Authorization": 'Bearer ' + ScriptApp.getOAuthToken(),
}
};
var response = UrlFetchApp.fetch(url, params);
SpreadsheetApp.getUi().alert(response);
var data = response.getContentText();
var json = JSON.parse(data);
//console.log(json);
var final=[];
for (var a=0; a<json.policies.length; a++)
{
var name=json.policies[a].name;
var setting=json.policies[a].setting.type;
var value=json.policies[a].setting.value;
final.push([name,setting,value])
}
settingsSheet.getRange(2,1,final.length,final[0].length).setValues(final);
}
How about modifying your showing script as follows?
It seems that the default value of pageSize
is 50. This is the reason for your current issue. So, in this modification, in order to retrieve all items, I used pageSize
and pageToken
.
function get_settings() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const settingsSheet = ss.getSheetByName("07. settings api");
const api_key = PropertiesService.getScriptProperties().getProperty('apikey');
const params = {
headers: {
"method": "get",
"Authorization": 'Bearer ' + ScriptApp.getOAuthToken(),
}
};
let pageToken = "";
var json = { policies: [] };
do {
const url = `https://cloudidentity.googleapis.com/v1beta1/policies?key=${api_key}&pageSize=100&pageToken=${pageToken}&fields=*`;
const response = UrlFetchApp.fetch(url, params);
const data = response.getContentText();
const obj = JSON.parse(data);
if (obj.policies.length > 0) {
json.policies.push(...obj.policies);
}
pageToken = obj.nextPageToken;
} while (pageToken);
// SpreadsheetApp.getUi().alert(`${json.policies.length} items were obtained.`);
console.log(`${json.policies.length} items were obtained.`);
var final = [];
for (var a = 0; a < json.policies.length; a++) {
var name = json.policies[a].name;
var setting = json.policies[a].setting.type;
var value = json.policies[a].setting.value;
final.push([name, setting, value])
}
settingsSheet.getRange(2, 1, final.length, final[0].length).setValues(final);
}