Wechat Python

Posted onby admin
  1. Python 3.x with the Falcon API framework and Ruby 2.3 with the Sinatra framework The WeChat sandbox, a free service to access the WeChat API during development ngrok, a free HTTP tunneling service to test server code on the local machine.
  2. WeChat Oauth python - Stack Overflow Tutorial about getting started with Wechat as an admin was implemented on php http://admin.wechat.com/wiki/index.php?title=GettingStarted.

WeChat is a social messaging application that works across different and multiple platforms. WeChat offers instant messaging, chat, video calls, and voice chats. The WeChat API and SDK allow developers to access and integrate the functionality of WeChat with other applications.

折腾:

Python send wechat message

期间,Flask中,用代码:

app.logger.debug(‘respTicket=%s’, respTicket)
app.logger.debug(‘type(respQrcode)=%s, respQrcode=%s’, type(respQrcode), respQrcode)
#app.logger.debug(‘type(respQrcodeContent)=%s, respQrcodeContent=%s’, type(respQrcodeContent), respQrcodeContent)
app.logger.debug(‘type(respQrcodeContent)=%s’, type(respQrcodeContent))
# save qrcode data to tmp file
app.logger.debug(“type(curTimestamp)=%s, curTimestamp=%s”, type(curTimestamp), curTimestamp)
app.logger.debug(“qrcodeFullPath=%s”, qrcodeFullPath)
f.write(respQrcodeContent)
app.logger.debug(“saved qrcode image to %s ok”, qrcodeFullPath)
with open(qrcodeFullPath, ‘rb’) as qrcodeFile:
app.logger.debug(“type(qrcodeFile)=%s, qrcodeFile=%s”, type(qrcodeFile), qrcodeFile)
uploadMediaResp = wechat.upload_media(media_type=”image”, media_file=qrcodeFile)
app.logger.debug(“uploadMediaResp=%s”, uploadMediaResp)

结果出错:

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:443]:
type(curTimestamp)=<type ‘float’>, curTimestamp=1472214535.85

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:445]:

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:449]:

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:452]:
type(qrcodeFile)=<type ‘file’>, qrcodeFile=<open file ‘/tmp/1472214535.847.jpg’, mode ‘rb’ at 0x7f88a7ee06f0>

<div–<——————————————————————————

r.apparent_encoding ascii
r.headers {‘date’: ‘Fri, 26 Aug 2016 12:28:56 GMT’, ‘connection’: ‘keep-alive’, ‘content-type’: ‘text/plain’, ‘content-length’: ’68’}
r.url https://api.weixin.qq.com/cgi-bin/media/upload?access_token=4XX0K5L3R-z_9b-MJ5o80YMqUTZ1XuW1aAxMNGtjFYHmiYCFlamst45HbXk-MdCoI_8sZUvOiLU4yju4sIyNIr500aL8QP8h7vkavN1QK1N-9IJ9kBYVeBuffcwGVohnHBKaABAOIR&type=image
r.text {“errcode”:41005,”errmsg”:”media data missing hint: [ScXL0536e292]”}
r= <Response [200]>
[2016-08-26 20:28:56 +0000] [6242] [ERROR] Error handling request /create_event_qrcode?eventId=10
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File “/usr/share/nginx/html/SIPEvents/sipevents/views.py”, line 453, in create_event_qrcode
uploadMediaResp = wechat.upload_media(media_type=”image”, media_file=qrcodeFile)
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/basic.py”, line 428, in upload_media
return self._upload_media_py2(media_type, media_file, extension)
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/basic.py”, line 453, in _upload_media_py2
‘media’: (filename, media_file, convert_ext_to_mime(extension))
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 104, in post
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 74, in request
File “/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py”, line 113, in _check_official_error
raise OfficialAPIError(errcode=json_data.get(‘errcode’), errmsg=json_data.get(‘errmsg’, ”))
OfficialAPIError: 41005: media data missing hint: [ScXL0536e292]

搜:

OfficialAPIError: 41005: media data missing hint

wechat 41005 media data missing hint

python 41005: media data missing hint

去看了看代码:

Wechat Python Api

可能是我自己的问题,

至少是:

我对于:

/root/Envs/SIPEvents/lib/python2.7/site-packages/wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py

把之前的:

def request(self, method, url, access_token=None, **kwargs):

的:

method=method,
**kwargs

加了headers,变成了:

r = requests.request(
headers = {
‘Content-Type’ : ‘application/json; charset=UTF-8’,
‘Accept-Type’ : ‘application/json; charset=UTF-8’
url=url,
)

所以,先去掉之前加上的这个headers再说。

然后就可以了。。。

[总结]

此处之所以用python的微信的SDK wechat-python-sdk的调用:

wechat.upload_media

去上传文件,之所以出错:

OfficialAPIError: 41005: media data missing hint: [ScXL0536e292]

是因为之前对于:

wechat_sdk-0.6.4-py2.7.egg/wechat_sdk/lib/request.py

调试期间,把:

method=method,
**kwargs

改为了:

r = requests.request(
headers = {
‘Content-Type’ : ‘application/json; charset=UTF-8’,
‘Accept-Type’ : ‘application/json; charset=UTF-8’
url=url,
)

导致此处post上传文件时,内部数据处理异常,估计是导致找不到media项了,

所以才报此错误。

解决办法是:

去掉即可,变为原先的:

method=method,
**kwargs

即可正常上传资源文件。

Python Wechat Bot

另外,根据官网:

的解释,请注意:

Wechat Python Sdk

上传资源文件时,注意不要超过大小限制:

“注意事项

上传的临时多媒体文件有格式和大小限制,如下:

  • 图片(image): 1M,支持JPG格式
  • 语音(voice):2M,播放长度不超过60s,支持AMRMP3格式
  • 视频(video):10MB,支持MP4格式
  • 缩略图(thumb):64KB,支持JPG格式

媒体文件在后台保存时间为3天,即3天后media_id失效。”

转载请注明:在路上 » [已解决]使用wechat-python上传图片的media出错:OfficialAPIError 41005 media data missing hint ScXL0536e292