0

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

这也不起作用。

4

2 回答 2

1

所以事实证明,multipart/form-data不适用于 Zoom API 照片上传,但您必须删除 Content-Type 并将其替换为'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 的人省去一些麻烦。

于 2020-02-18T19:15:27.017 回答
0

也许有相同的解决方案,但对于 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 上找到解决方案

于 2020-05-21T08:23:01.573 回答