问题描述
我正在测试如何使用带有 Google Apps 脚本的 Admin SDK Directory Service 更新用户图片,并具有以下功能:
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');
}
然而,它并不总是有效.每当 base64 编码字符串中有填充时,它就会失败.参考 Google 的文档(https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update),我对这些描述有点困惑.它说:
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:
- 等号 (=) 字符被替换为星号 (*).
- 对于填充,使用句点 (.) 字符代替RFC-4648 baseURL 定义,它使用等号 (=)填充.这样做是为了简化 URL 解析.
实际应该怎么做?(=) 用于 Base64 中的填充.那么,我应该使用 (*) 还是 (.)?我确实尝试用 (.) 替换 (=) 但没有运气.
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.
有人可以帮忙吗?
太奇怪了.当我不替换 (=) 时它有效.
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');
}
推荐答案
API 要求您使用 URL 安全的 base64 编码.完成base64编码后,尝试将/
替换为_
,将+
替换为-
.详情见:
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
这篇关于将字符串转换为 Web 安全的 Base64 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!