java调用qq登录_<Java> QQ登录OAuth开发

//页面返回信息

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值