我一直在绞尽脑汁试图让它与 Python 一起工作。我看到你可以使用 Curl 和 JavaScript 来做到这一点,但我不想离开 Python。阅读文档(尽管它们非常简单),它说您必须简单地将标题中的数据格式化为multipart/form-data
并将文件作为二进制文件发送。
import requests
userid = 'myuserid@place.com'
url = 'https://api.zoom.us/v2/users/{0}/picture'.format(userid)
jwt_token = '<supersecretkey>'
filepath = '/Users/me/Pictures/myprofilepic.jpg'
headers = {
'Content-Type': 'multipart/form-data',
'Authorization': 'Bearer {0}'.format(jwt_token)
}
files = [
('pic_file', open('<filepath>','rb'))
]
response = requests.request('POST', url, headers=headers, files=files)
print(response.status_code)
但是,此示例不起作用。我不断收到 500 个错误。我使用 Zoom 打开了一个支持案例,并收到了完全相同的代码来运行。我试图通过格式化和设置我的界限来解决这个问题。
import requests
import binascii
import os
import base64
jwt_token = '<supersecretkey>'
filepath = '/Users/me/Pictures/myprofilepic.jpg'
def encode_image_base64(filename):
with open(filename,'rb') as file:
data_read = file.read()
data = base64.b64encode(data_read)
return data
def base64_encode_multipart_formdata(name,filename,content_type):
base64image = encode_image_base64(filename)
boundary = binascii.hexlify(os.urandom(16)).decode('ascii')
body = '--{0}\r\nContent-Disposition: form-data; name="{1}"; filename="{2}"\r\nContent-Type: {3}\r\n\r\n{4}\r\n--{0}--'.format(boundary,name,filename,content_type,base64image)
content_type = "multipart/form-data; boundary={}".format(boundary)
return( body, content_type)
def main():
name = 'pic_file'
content_type = 'image/jpeg'
body , ct = base64_encode_multipart_formdata(name,filepath,content_type)
headers = {
'Content-Type': '{}'.format(ct),
'Authorization': 'Bearer {}'.format(jwt_token)
}
response = requests.post(url, headers=headers, data=body)
print(response.status_code)
这也不起作用。