最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]使用wechat-python上传图片的media出错:OfficialAPIError 41005 media data missing hint ScXL0536e292

Flask crifan 3700浏览 0评论

折腾:

[已解决]微信后台服务器如何发送图片给微信公众号的关注者用户

期间,Flask中,用代码:

respTicket = respQrcodeJson[‘ticket’]
app.logger.debug(‘respTicket=%s’, respTicket)
respQrcode = wechat.show_qrcode(respTicket)
app.logger.debug(‘type(respQrcode)=%s, respQrcode=%s’, type(respQrcode), respQrcode)
respQrcodeContent = respQrcode.content
#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
curTimestamp = time.time()
app.logger.debug(“type(curTimestamp)=%s, curTimestamp=%s”, type(curTimestamp), curTimestamp)
qrcodeFullPath = “/tmp/%.3f.jpg” % (curTimestamp)
app.logger.debug(“qrcodeFullPath=%s”, qrcodeFullPath)
with open(qrcodeFullPath, ‘wb’) as f:
    f.write(respQrcodeContent)
    f.close()
    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)
    qrcodeFile.close()

结果出错:

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]:
qrcodeFullPath=/tmp/1472214535.847.jpg

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

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

DEBUG in views [/usr/share/nginx/html/SIPEvents/sipevents/views.py:449]:
saved qrcode image to /tmp/1472214535.847.jpg ok

<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.encoding ISO-8859-1
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]”}
type(r)= <class ‘requests.models.Response’>
r= <Response [200]>
[2016-08-26 20:28:56 +0000] [6242] [ERROR] Error handling request /create_event_qrcode?eventId=10
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  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__
    return self.wsgi_app(environ, start_response)
  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
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.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
    **kwargs
  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
    self._check_official_error(response_json)
  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

上传多媒体文件,一直返回{“errcode”:41005,”errmsg”:”media data missing”} – 微信公众平台开发者问答系统

上传多媒体文件问题 返回{“errcode”:41005,”errmsg”:”media data missing”} – 微信公众平台开发者问答系统

上传临时图片成功,但是永久图片返回{“errcode”:41005,”errmsg”:”media data missing”} · Issue #234 · dodgepudding/wechat-php-sdk

使用上传临时素材接口的时候,返回 一段buffer · Issue #19 · node-webot/co-wechat-api

微信公众平台上传多媒体文件问题 – 开源中国社区

Return Codes – WeChat Official Account Admin Platform

wechat 41005 media data missing hint

关于php 5.5以上 CURL上传的问题(微信”errcode”:41005,”errmsg”,) | 姜糖水

XE7编写微信接口(新增素材文件)返回{“errcode”:41005,”errmsg”:”media data missing”}-CSDN论坛-CSDN.NET-中国最大的IT技术社区

‘{“errcode”:41005,”errmsg”:”media data missing”}’_博问_博客园

微信错误 ‘{“errcode”:41005,”errmsg”:”media data missing”}’_已解决_博问_博客园

微信返回常见错误码 – ShareSDK – Mob官方论坛

python  41005: media data missing hint

Request向微信上传中文文件失败出错 Python中文社区

PHP curl 上传文件版本兼容问题 – walter371 – 博客园

去看了看代码:

可能是我自己的问题,

至少是:

我对于:

/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):

的:

        r = requests.request(
            method=method,
            url=url,
            **kwargs
        )

加了headers,变成了:

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

所以,先去掉之前加上的这个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

调试期间,把:

        r = requests.request(
            method=method,
            url=url,
            **kwargs
        )

改为了:

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

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

所以才报此错误。

解决办法是:

去掉即可,变为原先的:

        r = requests.request(
            method=method,
            url=url,
            **kwargs
        )

即可正常上传资源文件。

另外,根据官网:

新增临时素材 – 微信公众平台开发者文档

的解释,请注意:

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

“注意事项

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

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

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

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.237 seconds, using 22.20MB memory