建了个人工具类的微信公众账号,想主动推动微信消息给自己的账号,可是微信没有提供公开的api,只能通过模拟登陆网页版的公众平台结合sae平台来对用户进行消息的推送。
废话不多说。下面是模拟登陆的思路和代码。我自己也是个初学真,做个记录,也希望能帮助到别人。
微信公众平台网址 https://mp.
以下操作使用使用chrome示范:
打开微信公众平台,输入账号密码,用F12键打开开发者工具,打开Network标签,点击preserve log upon navigation 使其变为红色按钮 ,不然跳转之后请求记录会被清除。
点击登录按钮,成功进入公众账号管理平台后可以找到login?lang=zh_cn请求。打击打开:
可以看到请求头和post 的数据,把各个请求头逐个加入请求头内里,密码应该是md5加密过的,可以直接复制post里加密后的密码到代码里。该请求返回的是json数据,使用json库处理。
正确返回的数据如下:
{
"Ret": 302,
"ErrMsg": "/cgi-bin/home?t=home/index&lang=zh_CN&token=xxxxxxxx",
"ShowVerifyCode": 0,
"ErrCode": 0
}
返回的token很重要,而且每次返回的token可能不一样。要在代码里设置变量保存。此外还要主要处理cookie,以下为登陆的代码:
#-*- coding:utf-8 -*-
import urllib
import urllib2
import cookielib
import json
cj=cookielib.LWPCookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#登陆
paras={'username':'xxxxxx@','pwd':'xxxxxxxxxxxxxxxxxxxx','imgcode':'','f':'json'}
req=urllib2.Request('https://mp./cgi-bin/login?lang=zh_CN',urllib.urlencode(paras))
req.add_header('Accept','application/json, text/javascript, */*; q=0.01')
req.add_header('Accept-Encoding','gzip,deflate,sdch')
req.add_header('Accept-Language','zh-CN,zh;q=0.8')
req.add_header('Connection','keep-alive')
req.add_header('Content-Length','79')
req.add_header('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
req.add_header('Host','mp.')
req.add_header('Origin','https://mp.')
req.add_header('Referer','https://mp./cgi-bin/loginpage?t=wxm2-login&lang=zh_CN')
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36')
req.add_header('X-Requested-With','XMLHttpRequest')
ret=urllib2.urlopen(req)
retread=ret.read()
print retread
token=json.loads(retread)
#print token['ErrMsg'][44:]
token=token['ErrMsg'][44:]
print token
接下就可以推送消息了。
进入用户管理,点击你要发送消息的用户,使用同样办法,找到请求 singlesend?t=ajax-response&lang=zh_CN ,得到请求头和post的数据。
推送消息的代码如下:
paras2={'type':'1','content':'xxxxx','error':'false','imgcode':'','tofakeid':'xxxx','token':token,'ajax':'1'}# content为你推送的信息,tofakeid为用户的唯一标示id,可在html代码里找到
req2=urllib2.Request('https://mp./cgi-bin/singlesend?t=ajax-response&lang=zh_CN',urllib.urlencode(paras2))
req2.add_header('Accept','*/*')
req2.add_header('Accept-Encoding','gzip,deflate,sdch')
req2.add_header('Accept-Language','zh-CN,zh;q=0.8')
req2.add_header('Connection','keep-alive')
#req2.add_header('Content-Length','77') 此行代码处理发送数据长度的确认,不要加。是个坑。
req2.add_header('Content-Type','application/x-www-form-urlencoded; charset=UTF-8')
req2.add_header('Host','mp.')
req2.add_header('Origin','https://mp.')
req2.add_header('Referer','https://mp./cgi-bin/singlemsgpage?msgid=&source=&count=20&t=wxm-singlechat&fromfakeid=150890&token=%s&lang=zh_CN'%token)
req2.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36')
req2.add_header('X-Requested-With','XMLHttpRequest')
#不加cookie也可发送
#req2.add_header('Cookie',cookie2)
ret2=urllib2.urlopen(req2)
#ret2=opener.open(req2)
print 'x',ret2.read()
That's all. 复制代码请注意缩进问题。