Recent searches


No recent searches

Bulk Updating End Users API Fails

Answered


Posted Mar 07, 2022

Hi All,

I am in the process of writing a script in Google Apps Script so that I can bulk update or create End Users via the API but can't seem to make it work. Granted my skills are somewhat lacking but figured i would at least attempt it and learn as I go.

My spreadsheet is starting off simple; I have tried to either create or update 1 or 2 records using the fields name, email, tags and alias.

Below is the code I am trying to use.

function UpdateUsers(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var [headers, ...rows] = sheet.getDataRange().getValues();
vardata = {}
varitems = []
rows.forEach(function(r) {
varobj={}
r.forEach(function (c, j) {
obj[headers[j]] = c
})
vardata = {}//moved
data['users'] = obj// moved this inside your loop
items.push(data) // pushed the object into the items array
})
 
Logger.log("Log JSON Stringify Items: " + JSON.stringify(items))
 
items.forEach(function(i) { // added this to loop over objects in items
varuser = 'myemailaddresshere/token';
varpwd = 'mytokenhere';
varoptions = {
'method' : 'post',
'headers': {
'Authorization': "Basic " + Utilities.base64Encode(user + ':' + pwd)
},
'payload' : JSON.stringify(i),
'contentType': 'application/json',
'muteHttpExceptions': true
};
UrlFetchApp.fetch(url, options);
Logger.log(i)
varresponse = UrlFetchApp.fetch(url, options);
Logger.log(response);
})

}
 
The error being returned is: {"error":"InvalidEndpoint","description":"Not found"}

 


1

4

4 comments

A brief follow up on this; i have since changed the POST to PUT but still getting invalid endpoints error

1


Another update (I'm like a dog with a bone on this one :-) )

I realised that my endpoint was still wrong and it should be 

/api/v2/users/update_many.json
 
Now the issue i am getting is this:
{ "error": { "title": "Invalid attribute", "message": "You passed an invalid value for the users attribute. Invalid parameter: users must be an array from api/v2/users/update_many" } }

1


If this is ever of use to anyone else I have solved my issue with help from Stack Overflow; updated script is below:

 

function updateManyUsers() {
varsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var [headers, ...rows] = sheet.getDataRange().getDisplayValues();
Logger.log([headers,rows]);
varusers = rows.map(r => {
vartemp = {};
headers.forEach((h, j) => {
if (r[j] != "") temp[h] = r[j];
});
returntemp;
});
varuser = 'morris.coyle@retacted/token';
varpwd = 'redacted';
varoptions = {
'method': 'PUT',
'headers': {
'Authorization': "Basic " + Utilities.base64Encode(user + ':' + pwd)
},
'payload': JSON.stringify({ users }),
'contentType': 'application/json',
'muteHttpExceptions': true
};
varresponse = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}

1


Hey Morris! I don't have anything helpful to add here, I'm just thrilled to see someone else fighting with Google Apps Script and Zendesk, and that you posted your solution. It took me a week just to get authentication working the first time I tried to set up a script.  Thank you for posting this, I've got a project coming up in a similar vein and you just saved me a LOT of time. 

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post