Package: wechat-requests
To make it easier to requests Wechat/Weixin API(mp, pay, etc.)both for py3 and py2, test coverage >90%UsageInstallpipinstallwechat-requestsMPApi>>>fromwechatimportauth>>>r=auth.get_mp_access_token('your appid','your appsecret')>>>r.access_token'ACCESS_TOKEN'>>>r.json{u'access_token':u'ACCESS_TOKEN',u'expires_in':7200}>>>r.textu'{"access_token":"ACCESS_TOKEN","expires_in":7200}'>>>fromwechatimportmpapi>>>mp=mpapi.formp(r.access_token)>>>user=mp.get('user/info',openid='o6_bmjrPTlm6_2sgVt7hMZOPfL2M')>>>user.is_failedFalse>>>user.headimgurlu'http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0'PaySeeWeChat Pay DOCfor detail>>>fromwechatimportpay>>>mppay=pay.for_merchant(mchid,signkey)>>>r=mppay.unifiedorder(\...body='test',...out_trade_no='20150806125346',...total_fee=88,...spbill_create_ip='123.12.12.123',...notify_url='http://www.weixin.qq.com/wxpay/pay.php'...)>>>r.is_failedFalse>>>r.prepay_idu'wx201410272009395522657a690389285100'>>>r=mppay.orderquery(out_trade_no='20150806125346')>>>r.trade_stateu'SUCCESS'If visit api which need client cert:>>>fromwechatimportpay>>>mppay=pay.for_merchant(mchid,signkey,client_cert='./apiclient_cert.pem',client_key='apiclient_key.pem')>>>r=mppay.refund(\...transaction_id='1217752501201407033233368018',...total_fee=88,...refund_fee=88...)>>>r.refund_idu'2008450740201411110000174436'WebOauthSeeWeChat Oauth DOCfor detail>>>fromwechatimportweb_auth>>>web_auth.build_authgrant_url('APPID','http://redirect_to')u'https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect'>>>result=web_auth.get_access_token('APPID','SECRET','CODE')>>>result.access_tokenu'ACCESS_TOKEN'>>>user_result=web_auth.get_user_info('OPENID',result.access_token)>>>user_result.unionidu'o6_bmasdasdsad6_2sgVt7hMZOPfL'>>>refresh_result=web_auth.refresh_access_token('APPID',result.refresh_token)>>>refresh_result.refresh_tokenu'REFRESH_TOKEN'Message Hanlde PipelineSeeWeChat Message DOCfor detail>>>fromwechat.messageimportnew_pipeline>>>message_pipeline=new_pipeline([handler_instance,'your.handler.path'])>>>reply_message_bytes=message_pipeline.handle('receive xml message')>>>fromwechat.messageimportXMLMessageBuilder>>>reply_message=XMLMessageBuilder.parse(reply_message_bytes)>>>reply_message.to_openidu'fromUser'Message CryptoSeeWeChat Crypt DOCfor detail>>>fromwechat.messageimportbuild_message_crypto_for>>>crypto=build_message_crypto_for('TOKEN','AES_KEY','APPID')>>>crypto.encrypt('MESSAGE_XML')<xml><ToUserName><![CDATA[ToUserName]]></ToUserName><FromUserName><![CDATA[FromUserName]]></FromUserName><CreateTime>1409735669</CreateTime><Encrypt><![CDATA[uK+DOe54WRa31zp4IZ9wn2nmmyGW/Zp2lWg8s66DsPJDn4lq9Vl8ExMoUAYffJZhVNnMOay4ggAp3RGHteCKVU7krd8BUnoCcaOLyqbl36FxJWffWiOl6Xv4Xdb5fmQKnvG9swv4eXpTlH+L96SUa1C0dRofRC6tHJDHMNPuCun1R2UvQJRAcwoTIqwoHPMqJTehW3ttrohjeqaS7W9Nln3kufTmbwtyaYdwxUPP6agbc0KDGe3NzVGCQooAEmgOxQJW7kp2Rw6P7mLx2Mvr46bpiB6BFtDcZgnrto7/BqHzyCk50FPLl1BQDH2SgTkOzirV5XExAt1p+uuDSBo0Hw==]]></Encrypt></xml>>>>crypto.decrypt('ENCRYPTED_MSG','SIGNATURE','timestamp','nonce')<xml><ToUserName>ToUserName></ToUserName><FromUserName>FromUserName</FromUserName><CreateTime>1519387094</CreateTime><MsgType>text</MsgType><MsgId>-1</MsgId><Content>justa(汉字)test</Content></xml>RequestResultAll api return RequestResult instance, it auto handle xml, jsontake r for example:r.name or r[‘name’]: to get wechat api return result field(both xml and json)r.text: to get raw wechat api returned body (unicode/py2, str/py3)r.response: Requests Response instancer.request: Requests PreparedRequest instance, for users to debug thelow level requestr.is_failed: whether wechat api raise errorr.errcode: if r.is_failedr.errmsg: if r.is_failed, error message for manAdvancedcomming soon…Feature SupportWechat Requests is based onrequestsandurllib3get,postfunction surpports allrequestssurpport, like headers,
timeout, etc.Documentationcoming soon…TODOwechat/wxpay third party platformcomponent api surpportwill published in two weeks