将字符串转换为Web安全的Base64格式python 3.x.

时间:2018-03-05 04:02:21

标签: python google-admin-sdk

我正在使用以下内容测试如何使用带有Python的Admin SDK目录服务更新用户图片:

filename = input('Drag And Drop File\n')
image = open(filename,'rb')
image_read = image.read()
image_encoder = base64.b64encode(image_read)
our_str = image_encoder.decode('utf-8')
new_str = our_str.replace('/', '_').replace('+', '-').replace('=', '*')

def update_photo():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('admin', 'directory_v1', http=http)
    #Update Photo
    eventx = {
        "photoData": new_str,
        "mimeType": "JPEG"
        }

    print('Updating photo')
    update_photo = service.users().photos().update(userKey='hidden@hiddendomain.com', body=eventx).execute()
    return update_photo

photo_updated = update_photo()
print (photo_updated)

我收到此错误

raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/admin/directory/v1/users/hidden@hiddendomain.com/photos/thumbnail?alt=json returned "Invalid value for ByteString: my base64 code

参考 (https://developers.google.com/admin-sdk/directory/v1/reference/users/photos/update),

0 个答案:

没有答案