在项目开发过程中,使用ShareSDK提供的第三方登录功能,后台传参中需要unionid
下面介绍unionid的获取
1 . 微信 unionid
的获取
微信 unionid
的获取很简单 :
Platform weChat = ShareSDK.getPlatform(Wechat.NAME);
String unionid =
weChat.getDb().get("unionid");
2 . QQ unionid
的获取
很多人认为 QQ 没有 unionid
, 其实 QQ 也有
Platform qq = ShareSDK.getPlatform(QQ.NAME);
String exportData = qq.getDb().exportData();//获取到db里面所有的内容
我们下面来看看,exportData里面都有什么内容:
其实exportData是一个json数据,格式化后如下图所示:
我们发现里面并没有unionid,那我们怎么获得QQ的unionid呢 ?
天无绝人之路,有一个接口可以获取QQ的unionid,需要QQ的token作为参数,token我们是可以拿到的,上图选中的就是token
接口:
https://graph.qq.com/oauth2.0/me
请求方式:GET
请求参数:
access_token QQ的token
unionid 传1即可
代码如下 :
/**
* 获取QQ的unionid
* @param plat
*/
private void getQQUnionid(Platform plat) {
Request request = new Request(Request.Method.GET, "https://graph.qq.com/oauth2.0/me?access_token=" + plat.getDb().getToken() + "&unionid=1", new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {}}) {
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
byte[] data = response.data;
String s = new String(data);
String[] split = s.split(":");
s = split[split.length - 1];
split = s.split("\"");
s = split[1];
unionid = s;
return Response.success(s, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(Object response) {
}
@Override
public int compareTo(Object another) {
return 0;
}
};
NetworkManager.addRequest(request);
}
通过 String s = new String(data);
我们可以得到返回的字符串信息如下:
返回的并不是json数据,但幸运的是里面有unionid,我通过字符串切割的方法把它取了出来。