标签:回调 ext res key 定向 oauth ati ase tpc
/** * Step 1: 重定向到sina微博登录,请求授权码CODE * https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code * * @param resp * @throws Exception */ @RequestMapping("/weibo/login") public void weiboLogin(HttpServletResponse resp) throws Exception { String authorizeUrl = baseAuthorizeUrl + "?client_id=" + appKey + "&redirect_uri=" + redirectUri + "&response_type=code"; resp.sendRedirect(authorizeUrl); }
/** * 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
标签:回调 ext res key 定向 oauth ati ase tpc
原文地址:http://www.cnblogs.com/gdufs/p/7203252.html