0x01-微信公众号开发(环境,调试)

1、公众号测试后台环境
http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2、公众号信息分析

公众号接收消息格式(从微信发送到公众号)
1)文本信息
TextMessage(OrderedDict([(u’ToUserName’,u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468725197′), (u’MsgType’, u’text’), (u’Content’, u’hajj’), (u’MsgId’, u’6308126688332557493′)]))
2)取消关注
UnsubscribeEvent(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468725643′), (u’MsgType’, u’event’), (u’Event’, u’unsubscribe’), (u’EventKey’, None)]))
3)关注
SubscribeEvent(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468725754′), (u’MsgType’, u’event’), (u’Event’, u’subscribe’), (u’EventKey’, None)]))
4)图片信息(通过菜单“图片”或者通过“拍摄”图片)
ImageMessage(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468725820′), (u’MsgType’, u’image’), (u’PicUrl’, u’http://mmbiz.qpic.cn/mmbiz/dZ4QUHDiapeDjCYJScoEstA8LicPjMIiaBicIic5ekHM1VMazH9o1pru1SHvIyqKb20ibrADQK8lGagWadGIzfWvRGyg/0′), (u’MsgId’, u’6308129364097182966′), (u’MediaId’, u’i1OO6bmBGepdlf7LHVq1Y-PkIk7fstCRCW134VN5yYQlBfjz16Y4kDdAPQaR1HUS’)]))
5)视频信息 (通过菜单“图片”上传视频,或者通过“拍摄”的视频)
VideoMessage(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468725997′), (u’MsgType’, u’video’), (u’MediaId’, u’n1ZEh8PnywSrcErQN0vCfGbNt3wtyI2f10ikI7f4jVPXTqv-TkYrAoaaPoqLgsDP’), (u’ThumbMediaId’, u’9lXrBnhZWzIZwyWop274OuQTi6EBLzc_cyuCKhVN7m_JC0peN5jYdvAySZBjXjbh’), (u’MsgId’, u’6308130124306394400′)]))
6)短视频信息(通过菜单“短视频”)
ShortVideoMessage(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468726012′), (u’MsgType’, u’shortvideo’), (u’MediaId’, u’EqTgpIUOVztQuIFMfAHMZhNUEgOlR0gLAQnjbn0avQdHElBnJbTErARdIGSdl-7t’), (u’ThumbMediaId’, u’fbDj18A8XnGfCoWJ2zJaXEBG_4abwM9DbMo1XlLJ_XCONkEvgNEZxvn4GLUR9-zK’), (u’MsgId’, u’6308130188730903844′)]))
7)位置信息(菜单“位置”)
LocationMessage(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468726187′), (u’MsgType’, u’location’), (u’Location_X’, u’40.059185′), (u’Location_Y’, u’116.416283′), (u’Scale’, u’15’), (u’Label’, u’\u5317\u4eac\u5e02\u660c\u5e73\u533a\u7acb\u6c64\u8def186\u53f7\u9f99\u5fb7\u5e7f\u573aF1\u5c42′), (u’MsgId’, u’6308130940350180678′)]))
8)连接信息(菜单“收藏”)
LinkMessage(OrderedDict([(u’ToUserName’, u’gh_237b9488f76b’), (u’FromUserName’, u’ok2hTwT4sdRfdIwXWxwse3GBqukM’), (u’CreateTime’, u’1468726270′), (u’MsgType’, u’link’), (u’Title’, u’\u4ee8\u5927\u5956\u72ec\u5ba0\u4e00\u4eba \u79bb\u4e0d\u5f00\u575a\u6301\u4e0e\u7528\u5fc3′), (u’Description’, u’\u6700\u8fd1\uff0c\u8fbd\u9633\u6587\u5723\u8def\u9500\u552e\u5385\u9891\u9891\u5f97\u5e78\u8fd0\u4e4b\u795e\u5782\u9752\u30026\u65e5\u5185\uff0c\u8be5\u5385\u4e0d\u4ec5\u964d\u843d\u4e86\u4e00\u4e2a25\u4e07\u5143\u5168\u56fd\u7d2f\u79ef\u5956\uff0c\u8fd8\u5206\u522b\u98d8\u843d\u4e0b\u4e00\u4e2a5.’), (u’Url’, u’http://mp.weixin.qq.com/s?__biz=MzAwMzUxMjQxNg==&mid=2649836741&idx=3&sn=be02636fad0c056a337d196e6cdc6bee&scene=0#rd’), (u’MsgId’, u’6308131296832466258′)]))
9)名片信息(不支持)

 

tornado 微信企业号,web页面中获取用户信息

# -*- coding: utf-8 -*-S
__author__ = ‘liwf’
import tornado.ioloop
import tornado.web
import os.path
#import torndb
import tornado.httpserver
import tornado.options
import datetime
from tornado.options import define, options
import uuid
import random
from wechatpy.enterprise.crypto import WeChatCrypto
from wechatpy.enterprise import parse_message, create_reply
from wechatpy.enterprise.client import WeChatClient

define(“port”, default=9902, help=”run port”, type=int)
define(“wx_TOKEN”, default=”O4cshzc”)
define(“wx_EncodingAESKey”, default=”LVu02oqwBD0DV28bN3x9PbPBSHctKIWiIWBWt2UMHC7″)
define(“wx_CorpId”, default=”wx3e98c043f34252f6″)
define(“wx_SecretTXL”, default=”NPP6zx7lIPZ334E9FV4fW22g_uM1h4QrF2lReCYz5vi_gDnf3NpJ85i_7iimyv3XuG”)
define(“wx_Agent_id”, default=”11″)

class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r’/wx/verify’,WxVerifyHandler),
(r’/’,IndexHandler)
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), “templates”),
static_path=os.path.join(os.path.dirname(__file__), “static”),
#xsrf_cookies=True,
#cookie_secret=”Ljfsoajeiwhalfj293283jlfadfsajfdh”,
login_url=”/auth/login”,
debug=True,
)
super(Application, self).__init__(handlers, **settings)
# Have one global connection to the blog DB across all handlers

self.wx_client = WeChatClient(options.wx_CorpId, options.wx_SecretTXL)
self.crypto = WeChatCrypto(options.wx_TOKEN, options.wx_EncodingAESKey, options.wx_CorpId)

class BaseHandler(tornado.web.RequestHandler):
@property
def db(self):
pass
@property
def crypto(self):
return self.application.crypto
@property
def wx_client(self):
return self.application.wx_client

class WxVerifyHandler(BaseHandler):
def get(self):
signature = self.get_argument(‘msg_signature’)
timestamp = self.get_argument(‘timestamp’)
nonce = self.get_argument(‘nonce’)
echostr = self.get_argument(‘echostr’)
try:
echostr = self.crypto.check_signature(
signature,
timestamp,
nonce,
echostr
)
print ‘echostr-jiemi:’,echostr
except Exception as e:
print ‘error:’
print e.message
print echostr
self.write(echostr)
def post(self):
body = self.request.body
signature = self.get_argument(‘msg_signature’)
timestamp = self.get_argument(‘timestamp’)
nonce = self.get_argument(‘nonce’)
msg = self.crypto.decrypt_message(
body,
signature,
timestamp,
nonce
)
print “msg:——-::::” + msg
msg = parse_message(msg)
print “msg parse_message————–:”
print msg
reply = None
if msg.event == ‘scancode_waitmsg’ and msg.key == ‘key_scan_baodao’:
#报名
print “报到处理…”
self.write(“xxx”)
class IndexHandler(BaseHandler):
def get(self):
code = self.get_argument(‘code’)
print ‘code:’,code
wxUser = None
userInfo = None
if code:
wxUser = self.wx_client.user.get_info(11,code)
print ‘user:’,wxUser
userInfo = self.wx_client.user.get(wxUser[‘UserId’])

self.render(‘index.html’, wxUser=userInfo)

def main():
tornado.options.parse_command_line()
http_server = Application()#tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()

if __name__ == “__main__”:
main()

遇到的问题:(可信域名中一定要精确到子域名才可以)
wechatpy

SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

解决:
vi /usr/lib/python2.7/site-packages/wechatpy/client/base.py
requests.get(‘https://kennethreitz.com’, verify=False)

:::::::::::::::::::

def _fetch_access_token(self, url, params):
“”” The real fetch access token “””
res = requests.get(
url=url,
params=params,
verify=False
)