錯誤原因:urllib3的老版本bug,卸載掉 requests,urllib3,從新安裝最新版的requests(此包內部依賴urllib3);
我從新安裝的是 requests==2.22.0 及 urllib3==1.25.3 包
上傳 臨時資源的代碼如下(此代碼支持 上傳的文件 和 用戶收到顯示的文件 都為中文的 情況):
def upload_temp_file(self, token, userid, file_abs_path, file_type, file_name): """ 上傳 臨時素材資源 接口文檔地址:https://work.weixin.qq.com/api/doc#90000/90135/90253 :param token: 企業微信的token :param userid: 用戶企業微信id :param file_abs_path: 需要上傳文件的絕對路徑 :param file_type: 文件類型 :param file_name: 用戶收到文件時顯示的文件名 :return:bool,str """ # 校驗 file_type support_file_type = ['image', 'voice', 'video', 'file'] if file_type not in support_file_type: log.error(f'error file type,only support {support_file_type}') return False, None # 文件名為中文的bug解決方法:https://blog.csdn.net/u013250071/article/details/82493892 file_upload_result = requests.post( 'https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type={}'.format(token, file_type), files={'media': (file_name, open(file_abs_path, 'rb'), '', {})} )
企業微信發送結果為:
總結:
1.對接第三方接口,通過代碼對接時出現問題,就用 postman等 工具進行對接,如果使用工具對接沒有問題,極大可能是 代碼問題,不是第三方平台垃圾;
2.別忘了 考慮可能 是 使用的開源包的bug,如果 百度或谷歌 關鍵字 (python 企業微信 上傳臨時素材 44001 中文 empty media data)找不到相關答案;就試着用其他關鍵字進行查找 如 (python requests post 中文文件);
3.實在找不到bug,就把用的包 卸載 從裝最新的,畢竟可能會 之前使用的是老版本的未修復bug的包
相關連接:
https://blog.csdn.net/u013250071/article/details/82493892