python weixin
支持微信小程序云开发
A Python client for the Weixin REST APIs
component | compatibility |
---|---|
Python | 2.*, 3.* |
pip install python-weixin
* requests
* simplejson
* six
* xmltodict
* pycryptodome
微信小程式登入文檔
Python 實作小程式登入註冊
from weixin import WXAPPAPI
from weixin . lib . wxcrypt import WXBizDataCrypt
api = WXAPPAPI ( appid = APP_ID ,
app_secret = APP_SECRET )
session_info = api . exchange_code_for_session_key ( code = code )
# 获取session_info 后
session_key = session_info . get ( 'session_key' )
crypt = WXBizDataCrypt ( WXAPP_APPID , session_key )
# encrypted_data 包括敏感数据在内的完整用户信息的加密数据
# iv 加密算法的初始向量
# 这两个参数需要js获取
user_info = crypt . decrypt ( encrypted_data , iv )
Weixin API 使用OAuth2 認證方式詳情請見: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
具體使用方法參考sample app
取得access token 後,可以使用token 取得使用者資訊等:
from weixin . client import WeixinAPI
scope = ( "snsapi_login" , )
api = WeixinAPI ( appid = APP_ID ,
app_secret = APP_SECRET ,
redirect_uri = REDIRECT_URI )
authorize_url = api . get_authorize_url ( scope = scope )
access_token = api . exchange_code_for_access_token ( code = code )
api = WeixinAPI ( access_token = access_token )
user = api . user ( openid = "openid" )
from weixin . client import WeixinMpAPI
scope = ( "snsapi_base" , )
api = WeixinMpAPI ( appid = APP_ID ,
app_secret = APP_SECRET ,
redirect_uri = REDIRECT_URI )
authorize_url = api . get_authorize_url ( scope = scope )
access_token = api . exchange_code_for_access_token ( code = code )
api = WeixinMpAPI ( access_token = access_token )
user = api . user ( openid = "openid" )
from weixin . pay import WeixinPay
wxpay = WeixinPay ( appid = 'appid' ,
mch_id = 'mchid' ,
notify_url = 'url' ,
partner_key = 'key' )
create_pay_info = {}
# 统一下单
wxpay . unifiedorder ( ** create_pay_info )
# 查询订单
wxapp . order_query ( out_trade_no = 'out_trade_no' )
# 企业付款
wxepay = WeixinEnterprisePay ( appid = 'appid' ,
mch_id = 'mchid' ,
mch_key = 'mch_key' ,
mch_cert = 'mch_cert' ,
partner_key = 'key' )
wxepay . transfers ( partner_trade_no ,
openid ,
amount ,
desc = u'结算' )
# 企业付款查询
wxepay_query = WeixinEnterprisePayQuery ( appid = 'appid' ,
mch_id = 'mchid' ,
mch_key = 'mch_key' ,
mch_cert = 'mch_cert' ,
partner_key = 'key' )
wxepay_query . gettransferinfo ( partner_trade_no )
參數 | 是否必須 | 說明 |
---|---|---|
button | 是 | 一級選單數組,個數應為1~3個 |
sub_button | 否 | 二級選單數組,個數應為1~5個 |
type | 是 | 選單的反應動作類型 |
name | 是 | 選單標題,不超過16個字節,子選單不超過40個位元組 |
key | click等點擊類型必須 | 選單KEY值,用於訊息介面推送,不超過128位元組 |
url | view類型必須 | 網頁鏈接,用戶點擊菜單可打開鏈接,不超過1024字節 |
media_id media_id類型和view_limited類型必須 呼叫新增永久素材介面返回的合法media_id |
被動回覆是在用戶發出請求後在respone 中包含的內容
訊息體見官方參考文件: 被動回覆用戶訊息
API 列表
訊息體見官方參考文件: 客服訊息
API 列表
訊息體見官方參考文件: 範本訊息