1.带参数的二维码
为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2种类型的二维码:
- 临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
- 永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
1.1 创建二维码ticket
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id),分别介绍临时二维码和永久二维码的创建二维码ticket过程。
临时二维码请求说明:
http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式:json POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
永久二维码请求说明:
http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN POST数据格式:json POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}} 或者也可以使用以下POST数据创建字符串形式的二维码参数: {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
返回说明:
正确的Json返回结果:
{"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==","expire_seconds":60,"url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"}
错误的Json返回示例:
{"errcode":40013,"errmsg":"invalid appid"}
1.2 通过ticket换取二维码
获取二维码ticket后,开发者可用ticket换取二维码图片。请注意,本接口无须登录态即可调用。
请求说明:
HTTP GET请求(请使用https协议) https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
1.3 代码实例
class QRCodeHandler(RequestHandler): @tornado.gen.coroutine def get(self): access_token = yield AccessToken.get_access_token() print "access_token", access_token url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s" % access_token scene_id = self.get_argument("scene_id") req_body = '{"expire_seconds": 7200, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}' % scene_id client = AsyncHTTPClient() req = HTTPRequest(url, method="POST", body=req_body) resp = yield client.fetch(req) if "errcode" in resp.body: self.write("error") else: resp_data = json.loads(resp.body) ticket = resp_data['ticket'] self.write('<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s">' % ticket)
2. 扫描带参数二维码
用户扫描带场景值二维码时,可能推送以下两种事件:
- 如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
- 如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。
2.1 用户未关注时,进行关注后的事件推送
推送XML数据包示例:
<xml><ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[subscribe]]></Event> <EventKey><![CDATA[qrscene_123123]]></EventKey> <Ticket><![CDATA[TICKET]]></Ticket> </xml>
2.2 用户已关注时的事件推送
推送XML数据包示例:
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[FromUser]]></FromUserName> <CreateTime>123456789</CreateTime> <MsgType><![CDATA[event]]></MsgType> <Event><![CDATA[SCAN]]></Event> <EventKey><![CDATA[SCENE_VALUE]]></EventKey> <Ticket><![CDATA[TICKET]]></Ticket> </xml>
2.3 代码
class WeChatHandler(WeChatBaseHandler): """微信接入接口""" def get(self): """开发者验证接口""" echostr = self.get_argument("echostr") self.write(echostr) def post(self): """收发消息接口""" req_xml = self.request.body req = xmltodict.parse(req_xml)['xml'] msg_type = req.get("MsgType") if "text" == msg_type: resp = { "ToUserName":req.get("FromUserName", ""), "FromUserName":req.get("ToUserName", ""), "CreateTime":int(time.time()), "MsgType":"text", "Content":req.get("Content", "") } elif "voice" == msg_type: resp = { "ToUserName":req.get("FromUserName", ""), "FromUserName":req.get("ToUserName", ""), "CreateTime":int(time.time()), "MsgType":"text", "Content":req.get("Recognition", u"未识别") } elif "event" == msg_type: if "subscribe" == req.get("Event"): resp = { "ToUserName":req.get("FromUserName", ""), "FromUserName":req.get("ToUserName", ""), "CreateTime":int(time.time()), "MsgType":"text", "Content":u"感谢您的关注!" } if None != req.get("EventKey"): resp["Content"] += u"场景值:" resp["Content"] += req.get("EventKey")[8:] elif "SCAN" == req.get("Event"): resp = { "ToUserName":req.get("FromUserName", ""), "FromUserName":req.get("ToUserName", ""), "CreateTime":int(time.time()), "MsgType":"text", "Content":u"您扫描的场景值为:%s" % req.get("EventKey") } else: resp = None else: resp = { "ToUserName":req.get("FromUserName", ""), "FromUserName":req.get("ToUserName", ""), "CreateTime":int(time.time()), "MsgType":"text", "Content":"I love you, itcast!" } if resp: resp_xml = xmltodict.unparse({"xml":resp}) else: resp_xml = "" self.write(resp_xml)