gusibi / python-weixin

微信(weixin|wechat) Python SDK 支持开放平台和公众平台 支持微信小程序云开发
https://github.com/gusibi/python-weixin/wiki/快速开始
Other
841 stars 230 forks source link

startswith first arg must be bytes or a tuple of bytes, not str #33

Closed bbbcs closed 5 years ago

bbbcs commented 5 years ago

支付调用统一支付接口的时候,我基本上只填了最基本的信息 ,不知道哪里出的问题。

File "/Users/ /lib/python3.6/site-packages/weixin/pay.py", line 464, in unifiedorder method, url, kwargs = self.prepare_request('POST', path, params) File "/Users/ /lib/python3.6/site-packages/weixin/pay.py", line 115, in prepare_request kwargs['data'] = smart_bytes(xmltodict.unparse(xml_dict)) File "/Users/ /lib/python3.6/site-packages/xmltodict.py", line 449, in unparse kwargs) File "/Users/ ** /lib/python3.6/site-packages/xmltodict.py", line 386, in _emit if ik.startswith(attr_prefix): TypeError: startswith first arg must be bytes or a tuple of bytes, not str

bbbcs commented 5 years ago

Python 3.6.2 python-weixin==0.4.3

gusibi commented 5 years ago

把数据使用 smart_bytes 转码一下试试?

bbbcs commented 5 years ago

找到问题了

def params_encoding(params, charset='utf-8'):
    newparams = {}
    for k, v in params.items():
        newparams[k] = smart_unicode(v)
    return newparams

这里key还是byte的,所以后面xmltodict 匹配attitude的时候 key startwith('@')的时候出错了 。。

limitfox commented 5 years ago

python3字符串处理逻辑和python2不太一样,改成: newparams[k.decode("utf-8")] = smart_unicode(v) 就好了。

limitfox commented 5 years ago

签名拼字符串的地方也要改一下: prestr += '%s=%s%s' % (k.decode('utf-8'), newparams[k].decode('utf-8'), delimiter)

gusibi commented 5 years ago

感谢 反馈 我测试一下加进去

yswtrue commented 5 years ago

同遇到该问题