企业微信,临时素材名称乱码问题解决方案?
废话不多说,直接给解决方案,艹 async def upload_file2wecom(file_name):
access_token = await get_wecom_token()
file_path = get_ppt_file_path(file_name)
url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={access_token}&type=file"
# Read the file content as binary
with open(file_path, 'rb') as f:
file_content = f.read()
# Create form data
m = MultipartEncoder(
fields={
'media': (file_name, file_content, 'application/octet-stream')
}
)
async with aiohttp.ClientSession() as session:
headers = {
'Content-Type': m.content_type
}
async with session.post(url, data=m.to_string(),headers=headers) as response:
if response.status == 200:
json_resp = json.loads(await response.text())
print(
"upload_file2wecom:",
f"result:{str(json_resp)}")
if json_resp["errcode"] == 0:
return json_resp["media_id"]
else:
return ""
else:
print(f"Failed to upload file. Response: {await response.text()}")
return ""