如何使用JWT的Python(multipart/formdata)将照片/图片上传到Zoom API?

2024-03-28 12:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在绞尽脑汁,试图让它与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)

这也行不通


Tags: nameimportformtokenformatdatatypecontent
2条回答

也许有相同的解决方案,但对于PowerShell? 我试图这样做,但出现了一个错误500:

$headers=@{}
$headers.Add("authorization", "Bearer JWT_KEY")
$response = Invoke-RestMethod -Uri 'https://api.zoom.us/v2/users/id/picture' -Method POST -Headers $headers -ContentType 'multipart/form-data' -Body '{"pic_file":"C:/test/pic.jpg"}'

更新。 在PowerShell 7.0上找到解决方案:

$headers=@{}
$headers.Add("authorization", "Bearer JWT_KEY")
$Form = @{pic_file     = Get-Item -Path '.\\test.jpg'}

$response = Invoke-RestMethod -Uri 'https://api.zoom.us/v2/users/id/picture' -Method POST -Headers $headers -Form $Form

但我想在PowerShell 5.0上找到一个解决方案

所以结果是,multipart/form-data不适用于Zoom API照片上载,而是必须删除内容类型并用'Accept': 'application/json'替换。简单的改变,但瞧!它现在将上传照片

import requests

userid = 'myuserid@place.com'
jwt_token = '<supersecretkey>'
filepath = '/Users/me/Pictures/myprofilepic.jpg'

url = 'https://api.zoom.us/v2/users/{0}/picture'.format(userid)

headers = {'Authorization': 'Bearer {}'.format(jwt_token),
           'Accept': 'application/json',
           }
files = {'pic_file': open(filepath, 'rb'))}

response = requests.post(url, files=files, headers=headers)
print response.content

我希望这能为使用Zoom API的人省去一些麻烦

相关问题 更多 >