- /**
- * Step 2: 登录回调,获取Code
- *
- * @param req
- * @throws Exception
- */
- @RequestMapping("/weibo/callback") public String qqCallback(HttpServletRequest req) throws Exception {
- String code = req.getParameter("code");
- //获取Access Token,并返回给页面
- return getAccessToken(code);
- }
3. 通过 授权 code 获取 access_token
- /**
- * Step 3: 通过code获取access_token
- *
- * @param resp
- * @param code 认证码
- * @return
- * @throws Exception
- */
- private String getAccessToken(String code) throws Exception {
- //拼接请求URL
- String tokenUrl = baseTokenUrl + "?client_id=" + appKey + "&client_secret=" + appSecret + "&grant_type=authorization_code&code=" + code + "&redirect_uri=" + redirectUri;
- //发送post请求,获取响应字符串
- String resp = RestHttpClient.sendHttpPostRequest(tokenUrl);
- return resp;
- }
baseTokenUrl 为 https://api.weibo.com/oauth2/access_token
4. 通过 token 和 uid 调用微博 api,例如查询微博用户信息
- /**
- * Step 4: 根据Token和用户ID获取用户信息
- * access_token:2.00AZrTrB5jlucE809b31fc07I9H_XC, uid:1706396054
- *
- * @param access_token
- * @param uid
- * @return
- * @throws Exception
- */
- @RequestMapping("/weibo/userInfo") public String getUserInfo(String access_token, String uid) throws Exception {
- //拼接请求URL
- String userInfoUrl = baseUserInfoUrl + "?access_token=" + access_token + "&uid=" + uid;
- System.out.println(userInfoUrl);
- //发送请求,获取响应字符串
- String resp = RestHttpClient.sendHttpGetRequest(userInfoUrl);
- //返回响应字符串
- return resp;
- }
baseUserInfoUrl 为 https://api.weibo.com/2/users/show.json
源码地址:https://github.com/13babybear/bounter-openapi
来源: http://www.bubuko.com/infodetail-2215617.html