I am testing how to update user picture using the Admin SDK Directory Service with Google Apps Scripts with the following function:
function updatePhoto(){
var fileId = 'XXXXXXXXXXXXXXXXXXX';
var b = DocsList.getFileById(fileId).getBlob();
var encoded = Utilities.base64Encode(b.getBytes());
encoded = encoded.replace(/\//g,'_').replace(/\+/g,'-').replace(/\=/g,'*');
AdminDirectory.Users.Photos.update({
"photoData": encoded },'[email protected]');
}
However, it doesn't always work. Whenever there is padding in the base64 encoded string, it fails. Referring to Google's document (https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update), I am a bit confused with the descriptions. It says:
What should be actually done? (=) is used for padding in Base64. So, should I use (*) or (.)? I did try to replace (=) with (.) but no luck.
Can anyone help?
It is so strange. It works when i do not replace (=).
function updatePhoto(){
var fileId = 'XXXXXXXXXXXXXXXXXXX';
var b = DocsList.getFileById(fileId).getBlob();
var encoded = Utilities.base64Encode(b.getBytes());
encoded = encoded.replace(/\//g,'_').replace(/\+/g,'-');
AdminDirectory.Users.Photos.update({
"photoData": encoded },'[email protected]');
}
The API requires you to use URL-safe base64 encoding. After doing the base64 encoding, try replacing / with _ and + with -. Details at:
https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update
I know that this is quite old, but I'm working in something similar and I want to share my solution. You can use this function to convert your string to a base64safeurl string:
protected static string Base64ForUrlEncode(string str)
{
StringBuilder result = new StringBuilder(Convert.ToBase64String(Encoding.ASCII.GetBytes(str)).TrimEnd('='));
result.Replace('+', '-');
result.Replace('/', '_');
return result.ToString();
}
More info here: http://www.codeproject.com/Tips/76650/Base-base-url-base-url-and-z-base-encoding
Note: This is C# code, so this solution is for .NET developments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With