Python代码
class WxMiniGame( object ): def __init__( self , midas_app_key, session_key) self .midas_app_key = midas_app_key self .session_key = session_key def _sort_params( self , data): return "&" .join([ "%s=%s" % i for i in sorted (data.items(), key = lambda m:m[ 0 ]) ]) def _init_sign( self , uri, req_data): sig_data = { "openid" : req_data.get( "openid" ), "appid" : req_data.get( "appid" ), "offer_id" : req_data.get( "offer_id" ), "ts" : req_data.get( "ts" ), "zone_id" : req_data.get( "zone_id" ), "pf" : req_data.get( "pf" ), } request_method = "POST" request_str = self ._sort_params(sig_data) unsign_str = "%s&org_loc=%s&method=%s&secret=%s" % ( request_str, uri, request_method, self .midas_app_key ) error_logger.error(unsign_str) sig = hmac.new( self .midas_app_key, unsign_str, hashlib.sha256)\ .hexdigest() ### 生成mp_sig sig_data.update({ "sig" : sig, "access_token" : req_data.get( "access_token" ), }) request_str = self ._sort_params(sig_data) unsign_str = "%s&org_loc=%s&method=%s&session_key=%s" % ( request_str, uri, request_method, self .session_key ) error_logger.error(unsign_str) mp_sig = hmac.new( self .session_key, unsign_str, hashlib.sha256)\ .hexdigest() req_data.update({ "sig" : sig, "mp_sig" : mp_sig, }) wmg = WxMiniGame( "my_midas_app_key" , "my_session_key" ) uri = "/cgi-bin/midas/getbalance" req_data = { "openid" : "my_open_id" , "appid" : "my_app_id" , "offer_id" : "my_offer_id" , "ts" : int (time.time()), "zone_id" : "zone_id" , "pf" : "android" , } wmg._init_sign(uri, req_data) |
返回错误
{'errcode': 90009, 'errmsg': 'mp_sig error hint: [v197dA0543shc1]'}
帮看看算法有没有问题
找到原因了,user_ip不需要参与签名。。。
你好,可以参考以下链接:小游戏虚拟支付沙箱测试报错
https://developers.weixin.qq.com/blogdetail?action=get_post_info&docid=0002808df54218d9e8f6f6c4c5b400&highline=90009