将字符串转换为网页安全的Base64格式 [英] Converting string to web-safe Base64 format
问题描述
我正在测试如何使用带有Google Apps脚本的Admin SDK目录服务使用以下功能更新用户图片:
函数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},'harry.potter@abc.edu.hk');
}
然而,它并不总是奏效。每当base64编码字符串中有填充时,就会失败。请参阅Google的文档( https://developers.google。 com / admin-sdk / directory / v1 / reference / users / photos / update ),我对这些描述有些困惑。它说:
- 等号(=)字符替换为星号(*)。
- 对于填充,使用句点(。)字符而不是
RFC-4648 baseURL定义,该定义对
填充使用等号(=)。这是为了简化URL解析。
实际应该做什么? (=)用于Base64中的填充。那么,我应该使用(*)还是(。)?我确实尝试用(。)替换(=),但没有运气。
任何人都可以帮忙吗?
这太奇怪了。它可以工作,当我不替换(=)。
pre $ 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},'harry.potter@abc.edu.hk');
}
_
和 + $ c $替换 /
c>与 -
。详细信息:
https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update
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 },'harry.potter@abc.edu.hk');
}
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:
- The equals sign (=) character is replaced with the asterisk (*).
- For padding, the period (.) character is used instead of the
RFC-4648 baseURL definition which uses the equals sign (=) for
padding. This is done to simplify URL-parsing.
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 },'harry.potter@abc.edu.hk');
}
解决方案 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
这篇关于将字符串转换为网页安全的Base64格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!