doraemonext/wechat-python-sdk

上传素材 upload_media接口 对media_file类型验证不完善

Closed this issue · 4 comments

测试过程中 发现 从数据库读取的二进制数据 无法通过验

我怎么一直都提示41005,能不能贴出上传临时素材的伪代码?

@l315276651 请提供具体代码?以及具体的二进制数据?

@wjy397

# -*- coding: utf-8 -*-
from wechat_sdk import WechatBasic, WechatConf
if __name__ == '__main__':
    conf = WechatConf(appid='wx...', appsecret='de...')
    basic = WechatBasic(conf=conf)
    img = open("test.jpg", "rb")
    print(basic.upload_media('image', img))

python3 中移除了 file 类型,所以需要更改.

    def _upload_media_py2(self, media_type, media_file, extension=''):
        if not isinstance(media_file, file) and not isinstance(media_file, six.StringIO):
            raise ValueError('Parameter media_file must be file object or StringIO.StringIO object.')
        if isinstance(media_file, six.StringIO) and not is_allowed_extension(extension.lower()):
            raise ValueError('Please provide \'extension\' parameters when the type of \'media_file\' is \'StringIO.StringIO\'.')

        if isinstance(media_file, file):

我将

if not isinstance(media_file, file) 

改成

if not hasattr(media_file, 'read')

还没测试有没有效