/** * 授权获取头条code * @return */ @GetMapping("/authorize") public void authorize(String phone,JSONObject json) throws Exception{ String url = "https://open.snssdk.com/auth/authorize"; String param = "response_type=code&client_key="+client_key+"&redirect_uri="+redirect_uri+"&auth_only=1&mobile_only=1"; JSONObject data = JSONUtil.parseObj(json.get("data")); String session_key = data.getStr("session_key");//登录后返回的session URL urls = new URL(url+"?"+param); HttpURLConnection con = (HttpURLConnection) urls.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("Cookie", "sessionid="+session_key); con.connect(); String code=null; if(con.getResponseCode() == 302){//302是重定向状态 String rurl = con.getHeaderField("Location"); log.info("重定向地址:{}",rurl); String pram = rurl.split("[?]")[1]; code = pram.split("[=]")[1]; } con.disconnect();// 断开连接 if(code!=null){saveInfo(code,phone,data);}else{throw new BusinessException("获取重定向地址code失败!");} }
转载于:https://my.oschina.net/zhangyaxin/blog/1801724