【python】企業微信上傳臨時素材(multipart/form-data; boundary=)

企業微信的api:https://work.weixin.qq.com/api/doc#90000/90135/90253
第一次接觸上傳文件方式,記錄一下

def post_file_to_wechat_get_media_id(filepath, filename, access_token):
    
    from requests_toolbelt import MultipartEncoder
    
    post_file_url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=file"

    m = MultipartEncoder(
        fields={filename: ('file', open(filepath + filename, 'rb'), 'text/plain')},
    )
    print(m)
    r = requests.post(url=post_file_url, data=m, headers={'Content-Type': m.content_type})
    print(r.text)
    
    return r.text['media_id']

注意:如果是需要上傳多個字段,看參考文章中的內容

參考文章:https://blog.51cto.com/hequan/1906922

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章