@Cesar
2016-01-16T16:33:54.000000Z
字数 6814
阅读 1479
Java
在微信中,将你删掉的好友是无法加入你创建的群聊的,而微信网页版也可以创建群聊,所以使用微信网页版的接口可以实现分辨一个好友是不是将你删除了。
- appid (可写死,wx782c26e4c19acffb)
- fun : new
- lang : zh-CN (中国地区)
- _ : 时间戳
// 参考代码:
// Java版本
public String getUUID(){
String url = "https://login.weixin.qq.com/jslogin?appid=%s&fun=new&lang=zh-CN&_=%s";
url = String.format(url, appID,System.currentTimeMillis());
httpGet = new HttpGet(url);
try {
response = httpClient.execute(httpGet);
entity = response.getEntity();
String result = EntityUtils.toString(entity);
logger.debug(result);
String[] res = result.split(";");
if (res[0].replace("window.QRLogin.code = ", "").equals("200")) {
uuid = res[1].replace(" window.QRLogin.uuid = ", "").replace("\"", "");
return uuid;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
# python版本
def getuuid():
global uuid
url = 'https://login.weixin.qq.com/jslogin'
params = {
'appid': 'wx782c26e4c19acffb',
'fun': 'new',
'lang': 'zh_CN',
'_': int(time.time()),
}
request = urllib2.Request(url=url, data=urllib.urlencode(params))
response = urllib2.urlopen(request)
data = response.read()
regx = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"'
pm = re.search(regx, data)
code = pm.group(1)
uuid = pm.group(2)
if code == '200':
return True
return False
- uuid
//java版本
// 如果忽略注释直接返回获取图片的url放在网页中的<img>的标签下可以直接显示,如果使用注释中的内容会将其下载为本地图片
public String getQR(String uuid) {
if (uuid == null || "".equals(uuid)) {
return null;
}
String QRurl = "http://login.weixin.qq.com/qrcode/" + uuid;
logger.debug(QRurl);
return QRurl;
// 同时提供使其变为本地图片的方法
// httpGet = new HttpGet(QRurl);
// response = httpClient.execute(httpGet);
// entity = response.getEntity();
// InputStream in = entity.getContent();
// //注意这里要对filepath赋值
// OutputStream out = new FileOutputStream(new File("FilePath"+".png"));
// byte[] b = new byte[1024];
// int t;
// while((t=in.read())!=-1){
// out.write(b, 0, t);
// }
// out.flush();
// in.close();
// out.close();
}
# Python版本
def showQRImage():
global tip
url = 'https://login.weixin.qq.com/qrcode/' + uuid
request = urllib2.Request(url=url)
response = urllib2.urlopen(request)
f = open(QRImagePath, 'wb')
f.write(response.read())
f.close() # 保存到本地
- uuid : 就是之前获得的uuid
- _ : 时间戳
- tip : 判断是要获得点击状态还是扫描状态
- 状态=200时,返回值是redirect_url:该返回值是一个url,访问该url就算是正式的登陆。
//java版本
public int waitForLogin(String uuid, int tip) {
String urlString = "http://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s";
urlString = String.format(urlString, tip, uuid, System.currentTimeMillis());
httpGet = new HttpGet(urlString);
try {
response = httpClient.execute(httpGet);
String re = EntityUtils.toString(response.getEntity());
String[] result = re.split(";");
logger.debug(re);
if (result[0].replace("window.code=", "").equals("201")) {
tip = 0;
return 201;
} else if (result[0].replace("window.code=", "").equals("200")) {
redirectUri = (result[1].replace("window.redirect_uri=", "").replace("\"", "") + "&fun=new").trim();
return 200;
} else {
return 400;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
# python版本
def waitForLogin():
global tip, base_uri, redirect_uri
url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % (tip, uuid, int(time.time()))
request = urllib2.Request(url = url)
response = urllib2.urlopen(request)
data = response.read()
regx = r'window.code=(\d+);'
pm = re.search(regx, data)
code = pm.group(1)
if code == '201': #已扫描
print '成功扫描,请在手机上点击确认以登录'
tip = 0
elif code == '200': #已登录
regx = r'window.redirect_uri="(\S+?)";'
pm = re.search(regx, data)
redirect_uri = pm.group(1) + '&fun=new'
base_uri = redirect_uri[:redirect_uri.rfind('/')]
elif code == '408': #超时
pass
return code
int ret;//返回值为0时表示本次请求成功
String message;//一些信息(比如失败原因等)
String skey;//后面请求会用到的参数
String wxsid;//同上
Long wxuin;// 本人编码
String pass_ticket;//重要!!后面很多请求都会用到这张通行证
int isgrayscale;//不明
代码如下:
//java
private boolean login() {
String url = redirectUri;
httpGet = new HttpGet(url);
try {
response = httpClient.execute(httpGet);
entity = response.getEntity();
String data = EntityUtils.toString(entity);
logger.debug(data);
loginResponse = CommonUtil.parseLoginResult(data);
baseRequest = new BaseRequest(loginResponse.getWxuin(), loginResponse.getWxsid(), loginResponse.getSkey(),
loginResponse.getDeviceID());
return true;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
#python版本
def login():
global skey, wxsid, wxuin, pass_ticket, BaseRequest
request = urllib2.Request(url = redirect_uri)
response = urllib2.urlopen(request)
data = response.read()
doc = xml.dom.minidom.parseString(data)
root = doc.documentElement
for node in root.childNodes:
if node.nodeName == 'skey':
skey = node.childNodes[0].data
elif node.nodeName == 'wxsid':
wxsid = node.childNodes[0].data
elif node.nodeName == 'wxuin':
wxuin = node.childNodes[0].data
elif node.nodeName == 'pass_ticket':
pass_ticket = node.childNodes[0].data
if skey == '' or wxsid == '' or wxuin == '' or pass_ticket == '':
return False
BaseRequest = {
'Uin': int(wxuin),
'Sid': wxsid,
'Skey': skey,
'DeviceID': deviceId,
}
return True
- pass_ticket
- skey 这两个参数都是login时的返回值之一
- r 时间戳
post 携带:- BaseRequst=Json格式的BaseRequest,该类参数如下:long Uin;String Sid;String Skey;String DeviceID; 注意第一个字母一定要大写。deviceid是一串e开头的随机数,随便填就可以。
//java
private void initWX() {
String url = String.format("http://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=%s&skey=%s&r=%s",
loginResponse.getPass_ticket(), loginResponse.getSkey(), System.currentTimeMillis());
InitRequestJson initRequestJson = new InitRequestJson(baseRequest);//Java中包含了BaseRequest的包装类
String re = getResponse(url, gson.toJson(initRequestJson));//这是自己写的一个公有方法,可以直接看源码
InitResponseJson initResponseJson = gson.fromJson(re, InitResponseJson.class);
mine = initResponseJson.getUser();// 获取当前用户信息
}
def webwxinit():
url = base_uri + '/webwxinit?pass_ticket=%s&skey=%s&r=%s' % (pass_ticket, skey, int(time.time()))
params = {
'BaseRequest': json.dumps(BaseRequest)
}
request = urllib2.Request(url=url, data=json.dumps(params))
request.add_header('ContentType', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
data = response.read()
global ContactList, My
dic = json.loads(data)
ContactList = dic['ContactList']
My = dic['User']
ErrMsg = dic['BaseResponse']['ErrMsg']
if len(ErrMsg) > 0:
print ErrMsg
Ret = dic['BaseResponse']['Ret']
if Ret != 0:
return False
return True