====== 微博用户信息获取整理 ======
===== 前提 =====
* 准备微博账号:cinvestors_app@tebon.com.cn
* 登录微博开发平台,http://open.weibo.com/,添加中国好投资应用
* 后台查看应用的App Key和App Secret
===== 用户信息获取 =====
==== 获取code ====
get请求(页面跳转)
注意:
- 微博H5已经登录,当前浏览器保留登录cookie,否则跳到微博登录页面
- client\_id为App Key
- redirect\_uri为授权回调地址,需要做url转码,微博开发平台后端可以修改,如下图:
{{:分享:技术:微博:1.png?800|}}
https://api.weibo.com/oauth2/authorize?client_id=1570065866&response_type=code&redirect_uri=https%3A%2F%2Fapi.weibo.com%2Foauth2%2Fdefault.html
请求成功,会跳转到redirect\_uri同时带上code
https://api.weibo.com/oauth2/default.html?code=d47ad7eeaf07901b9372645b3777a05c
==== 获取access_token和uid ====
根据code做post请求(可以从服务端请求)
注意:
- client\_id为App Key
- client\_secret为App Secret
- code为上一步获取的code
- redirect\_uri为授权回调地址,不做url转码,微博开发平台后端可以修改,如下图:
String url = "https://api.weibo.com/oauth2/access_token";
Map mapData = new HashMap();
mapData.put("client_id", "1570065866");
mapData.put("client_secret", "388725da138272e2d577fbcf480816ea");
mapData.put("grant_type", "authorization_code");
mapData.put("redirect_uri", "https://api.weibo.com/oauth2/default.html");
mapData.put("code", "d47ad7eeaf07901b9372645b3777a05c");
String content = HttpClientUtils.getWebContentByPost(url, mapData, "GBK");
System.out.println("content=" + content);
返回json,服务端解析得到access\_token和uid
{"access_token":"2.00eaYP_GArpPiB597d053aa6S3d7FD","remind_in":"157679999","expires_in":157679999,"uid":"5781255264"}
===== 参考帖子 =====
http://open.weibo.com/wiki/Oauth2/authorize
http://open.weibo.com/wiki/Oauth2/access_token