//页面返回信息
String nickname = "Unkown";//json处理工具
ObjectMapper objectMapper = newObjectMapper();//请求执行工具
CloseableHttpClient httpclient =HttpClients.createDefault();//请求发送信息
HttpPost httppost;
HttpEntity reqEntity;
CloseableHttpResponse responseEntity;//请求返回值处理需要的临时变量
BufferedReader reader;
StringBuilder tempSB;
String tempLine;//处理结果
Mapmap;try{
Map param = objectMapper.readValue(new File(request.getServletContext().getRealPath("/") + "webfile/qqinfo.json"), Map.class);
String codeStr= request.getParameter("code");//如果code或json信息为空直接退出
if (codeStr != null && param != null) {//第一次请求
httppost = new HttpPost("https://graph.qq.com/oauth2.0/token");
reqEntity= MultipartEntityBuilder.create().addPart("code", new StringBody(codeStr, ContentType.TEXT_PLAIN)).addPart("client_id", new StringBody(param.get("qq_id"), ContentType.TEXT_PLAIN)).addPart("client_secret", new StringBody(param.get("qq_secret"), ContentType.TEXT_PLAIN)).addPart("redirect_uri", new StringBody(param.get("qq_url"), ContentType.TEXT_PLAIN)).addPart("grant_type", new StringBody("authorization_code", ContentType.TEXT_PLAIN)).build();
httppost.setEntity(reqEntity);
responseEntity=httpclient.execute(httppost);//从第一次请求的返回值中拿到token
String accessToken = null;
tempSB= newStringBuilder();
reader= new BufferedReader(newInputStreamReader(responseEntity.getEntity().getContent()));while ((tempLine = reader.readLine()) != null) {
tempSB.append(tempLine);
}
String[] results= tempSB.toString().split("&");for(String result : results) {
String[] tmp= result.split("=");if (tmp.length == 2 && "access_token".equals(tmp[0])) {
accessToken= tmp[1];
}
}//如果token拿不到直接退出
if (accessToken != null) {//第二次请求
httppost = new HttpPost("https://graph.qq.com/oauth2.0/me");
reqEntity= MultipartEntityBuilder.create().addPart("access_token", newStringBody(accessToken, ContentType.TEXT_PLAIN)).build();
httppost.setEntity(reqEntity);
responseEntity=httpclient.execute(httppost);//从第二次请求的返回值中拿到openid
tempSB = newStringBuilder();
reader= new BufferedReader(newInputStreamReader(responseEntity.getEntity().getContent()));while ((tempLine = reader.readLine()) != null) {
tempSB.append(tempLine);
}
map= objectMapper.readValue(tempSB.toString().substring(tempSB.toString().indexOf("{"), tempSB.toString().indexOf("}") + 1), Map.class);//如果openid拿不到则直接退出
if (map.get("client_id") != null && map.get("openid") != null) {//第三次请求
httppost = new HttpPost("https://graph.qq.com/user/get_user_info");
reqEntity= MultipartEntityBuilder.create().addPart("access_token", new StringBody(accessToken, ContentType.TEXT_PLAIN)).addPart("openid", new StringBody(map.get("openid"), ContentType.TEXT_PLAIN)).addPart("oauth_consumer_key", new StringBody(map.get("client_id"), ContentType.TEXT_PLAIN)).build();
httppost.setEntity(reqEntity);
responseEntity=httpclient.execute(httppost);//从第三次请求的返回值中拿到用户信息(nickname)
tempSB = newStringBuilder();
reader= new BufferedReader(newInputStreamReader(responseEntity.getEntity().getContent()));while ((tempLine = reader.readLine()) != null) {
tempSB.append(tempLine);
}
map= objectMapper.readValue(tempSB.toString(), Map.class);
nickname= map.get("nickname");
}
}
}
}catch(Exception e) {
e.printStackTrace();
}