原创
通过接口获取QQ头像和昵称
一、获取QQ昵称
获取QQ昵称是通过QQ空间的一个Api接口(JSONP格式)获取
http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=760613041
示范:
二、获取QQ头像
可以通过下面这两个链接中的任意一个
http://q1.qlogo.cn/g?b=qq&nk=760613041&s=300
http://q2.qlogo.cn/headimg_dl?dst_uin=760613041&spec=40
示范:
其中nk为QQ号,s为头像的尺寸
参数s | 对应的尺寸 |
---|---|
1 | 40 × 40 |
2 | 40 × 40 |
3 | 100 × 100 |
4 | 140 × 140 |
5 | 640 × 640 |
40 | 40 × 40 |
100 | 100 × 100 |
三、Java获取、包装数据
@PostMapping("/qq/{qq}")
public ResponseVO qq(@PathVariable("qq") String qq) {
if (StringUtils.isEmpty(qq)) {
return ResultUtil.error("");
}
Map<String, String> resultMap = new HashMap<>(4);
String nickname = "匿名";
String json = RestClientUtil.get("http://users.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?uins=" + qq, "GBK");
if (!StringUtils.isEmpty(json)) {
try {
json = json.replaceAll("portraitCallBack|\\\\s*|\\t|\\r|\\n", "");
json = json.substring(1, json.length() - 1);
LOG.info(json);
JSONObject object = JSONObject.parseObject(json);
JSONArray array = object.getJSONArray(qq);
nickname = array.getString(6);
} catch (Exception e) {
LOG.error("通过QQ号获取用户昵称发生异常", e);
}
}
resultMap.put("avatar", "https://q1.qlogo.cn/g?b=qq&nk=" + qq + "&s=40");
resultMap.put("nickname", nickname);
resultMap.put("email", qq + "@qq.com");
resultMap.put("url", "https://user.qzone.qq.com/" + qq);
return ResultUtil.success(null, resultMap);
}
注:RestClientUtil类为普通的发起HTTP请求的工具类,可自己实现,也可在文末的源码链接中获取
- 本文作者: lzhpo
- 本文链接: http://www.lzhpo.com/article/66
- 版权声明: 本文为本人原创文章,采用 CC BY 3.0 CN协议 ,可自由转载、引用,但需署名作者且注明文章出处。
正文到此结束