I’m develping an App with Google Apps Script and I need change the profile user’s photo from a specific user.
The flow is follow:
HR person will chose a photo from your computer and click in the save button for save photo in the Slack profile’s photo.
I’m trying to use the users.setPhoto for change the user’s photo but it throw a lot of diferent errors, like bad_image
, invalid_arg_name
.
On front-end I’m sending the image:
$('#upload_form').submit(function (e) {
e.preventDefault()
var fileInput = document.getElementsByClassName('input_file');
var originalFile = fileInput[0].files[0]
var newFileName = email.split('@')[0] + '.' + originalFile.name.split('.').pop()
var newFile = new File([originalFile], newFileName, { type: originalFile.type })
console.log('newFile', newFile)
var reader = new FileReader()
reader.onload = (e) => {
var content = e.target.result
google.script.run.withSuccessHandler(function(result) {
console.log(result)
}).withFailureHandler(function(e) {
console.log(e)
}).test(content)
}
var newFileRead = reader.readAsDataURL(newFile)
})
On the back-end I’m receveiding the image and sending an post request to https://slack.com/api/users.setPhoto
function updateUserProfilePhoto(userId, image) {
var accessToken = PropertiesService.getScriptProperties().getProperty('ACCESS_TOKEN')
var url = 'https://slack.com/api/users.setPhoto'
Logger.log('image: ' + JSON.stringify(image))
var payload = {
image: image,
user_id: userId
}
var options = {
method: 'post',
headers: {
Authorization: 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
},
payload: payload,
muteHttpExceptions: true
};
try {
var response = UrlFetchApp.fetch(url, options);
var jsonResponse = JSON.parse(response.getContentText());
Logger.log(jsonResponse);
if (jsonResponse.ok) {
Logger.log('Photo update successful');
} else {
Logger.log(`Error on update profile's photo: ${jsonResponse.error}` );
}
} catch (error) {
Logger.log('Error on request: ' + error.toString());
}
}
I already tried change the “Content-Type” and a lot of the thing in the script but, no worked yet.
Dieferson Oliveira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.