Created
February 5, 2015 07:12
-
-
Save thm1118/58223a6c8b3afb23cd7a to your computer and use it in GitHub Desktop.
微信创建菜单,获取二维码工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import os | |
| import urllib2 | |
| import time | |
| try: | |
| import simplejson as json | |
| except ImportError: | |
| import json | |
| #微信服务创建或修改菜单的频度很低,获取永久二维码也只需要一次性即可,因此用python简单实现即可,需要时执行一下。 | |
| #1、配置appid_secret | |
| #2、需要首先运行get_access_token()来获得access_token,再粘贴在对应变量中。 | |
| #3、执行create_menu() 创建菜单 | |
| #4、执行get_qrcode_withparam() 获得永久二维码 | |
| #微信的appid和secret | |
| appid_secret = r'appid=XXXX&secret=XXXXX' | |
| access_token = r'IhIRD7PAK0rMRuD7clHzdB1pTJ7i08y6MIdOSCnxhfkO1QpiiLxlIFE4yp1HATK2eK2SYq0IlMj20wTpDZxYd1KSfbXRmD0-C5nAl1QpBIc' | |
| expires = int(time.time()) | |
| def get_json(url, data=None): | |
| req = urllib2.Request(url, data=data) | |
| content = urllib2.urlopen(req).read() | |
| result = json.loads(content) | |
| if check_error(result): | |
| return result | |
| else: | |
| return None | |
| def check_error(json_result): | |
| if not json_result: | |
| return False | |
| if not isinstance(json_result, dict): | |
| return False | |
| errcode = json_result.get("errcode") | |
| if errcode: | |
| print u"发生错误:", str(errcode), (json_result.get("errmsg")) | |
| return False | |
| return True | |
| # 获取access_token ,需要记录过期时间,默认7200秒,需要注意缓存。 | |
| def get_access_token(): | |
| url = r'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&' + appid_secret | |
| result = get_json(url) | |
| if not result: | |
| return | |
| access_token = result.get("access_token") | |
| expires = result.get("expires_in") + int(time.time()) | |
| print access_token | |
| def get_users(): | |
| url = r'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' + access_token | |
| result = get_json(url) | |
| if not result: | |
| return | |
| count = result.get("count") | |
| total = result.get("total") | |
| openids = result.get("data").get("openid") | |
| for openid in openids: | |
| get_user_info(openid) | |
| print result | |
| print "count:", count | |
| print "total", total | |
| print "openids", openids | |
| def get_user_info(openid): | |
| url = r'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + access_token + '&openid=' + openid + '&lang=zh_CN' | |
| result = get_json(url) | |
| if not result:return | |
| print "user_info:", result | |
| # 生成带参数的二维码 | |
| def get_qrcode_withparam(): | |
| url = r'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' + access_token | |
| for scene_id in range(26600, 26601): | |
| #data = {"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene": scene_id}}} | |
| data = {"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": scene_id}}} | |
| jsondata = json.dumps(data) | |
| #print "jsondata", jsondata | |
| result = get_json(url, data=jsondata) | |
| if not result: | |
| return | |
| # {"ticket":"gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==", | |
| # "expire_seconds":60, | |
| # "url":"http:\/\/weixin.qq.com\/q\/kZgfwMTm72WWPkovabbI"} | |
| #print result | |
| qr_ticket = result.get("ticket") | |
| qr_url = result.get("url") | |
| #print u"二维码为:", result | |
| get_qrcode_from_ticket(scene_id, qr_ticket) | |
| # 根据生成二维码获得的ticket,重新获取二维码链接 | |
| def get_qrcode_from_ticket(scene_id, qr_ticket): | |
| url = r'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' + urllib2.quote(qr_ticket) | |
| #result = get_json(url) | |
| req = urllib2.Request(url) | |
| result = urllib2.urlopen(req).read() | |
| if not result: | |
| return | |
| #ticket正确情况下,http 返回码是200,是一张图片,可以直接展示或者下载。 | |
| qr_title = str(scene_id)+".png" | |
| with open(os.path.abspath('.')+'/qrcodeimgs/'+qr_title, 'wb') as code: | |
| code.write(result) | |
| print qr_title, qr_ticket | |
| def create_menu(): | |
| # menu = r'{"button": [{"name": "问题反馈","type": "click","key": "问题反馈"},{"name": "关于我们","sub_button": ' \ | |
| # r'[{"type": "click","name": "公司简介","key": "公司简介"},{"type": "click","name": "公司愿景",' \ | |
| # r'"key": "公司愿景"},{"type": "click","name": "经营理念","key": "经营理念"},{"type": "click","name": "联系我们",' \ | |
| # r'"key": "联系我们"}]} ]}' | |
| # 河络服务菜单 | |
| #menu = r'{"button": [{"name": "问题反馈","type": "click","key": "问题反馈"},{"type": "click","name": "关于河络","key": "公司简介"}]}' | |
| # 人才服务中心菜单 | |
| menu = r' { "button":[ { "name":"人才动态", "sub_button":[ { "type":"view", "name":"更多新闻", "url":"http://eheluo.f3322.net:9999/www/index.html#/news/new_list" }, { "type":"view", "name":"中心活动", "url":"http://eheluo.f3322.net:9999/www/index.html#/news/new_list" }, { "type":"view", "name":"应届生就业", "url":"http://eheluo.f3322.net:9999/www/index.html#/news/new_list" }] }, { "name":"业务大厅", "sub_button":[ { "type":"view", "name":"我要申请", "url":"http://eheluo.f3322.net:9999/www/index.html#/appointment/appointment_list" }, { "type":"view", "name":"我要预约", "url":"http://eheluo.f3322.net:9999/www/index.html#/appointment/appointment_list" }, { "type":"view", "name":"我要查询", "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxcd571eea34232e8f&redirect_uri=http%3A//eheluo.f3322.net%3A9999/www/index.html%23/home/home&response_type=code&scope=snsapi_base&state=woyaochaxun#wechat_redirect" }] }, { "name":"业务指南", "sub_button":[ { "type":"view", "name":"业务资讯", "url":"http://eheluo.f3322.net:9999/www/index.html#/policy/policy_list" }, { "type":"view", "name":"中心介绍", "url":"http://eheluo.f3322.net:9999/www/index.html#/policy/policy_list" }] } ] }' | |
| #jsondata = json.dumps(menu) | |
| #jsondata = urllib2.quote(jsondata) | |
| print "menujson:", menu | |
| url = r' https://api.weixin.qq.com/cgi-bin/menu/create?access_token='+access_token | |
| result = get_json(url, data=menu) | |
| if not result: | |
| return | |
| print result | |
| errcode = result.get("errcode") | |
| print errcode | |
| # | |
| get_access_token() | |
| #create_menu() | |
| #get_users() | |
| #get_qrcode_withparam() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment