设为首页 加入收藏

TOP

github认证登陆(二)
2019-09-30 16:48:15 】 浏览:81
Tags:github 认证 登陆
设置从GitHub那里获取的数据设置成josn格式 MediaType mediaType = MediaType.get("application/json; charset=utf-8"); OkHttpClient client = new OkHttpClient(); //通过okhttclient来获取github传过来的用户信息,要利用到AceessTokenDTO内的数据! //将传过来的accesstokendto转化为json格式,通过post形式传给github RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(accessTokenDTO)); Request request = new Request.Builder() .url("https://github.com/login/oauth/access_token") .post(body) .build(); try (Response response = client.newCall(request).execute()) { String string = response.body().string(); //把传过来的token解析 String token = string.split("&")[0].split("=")[1]; return token; } catch (Exception e) { //log.error("getAccessToken error,{}", accessTokenDTO, e); } return null; } ? public GithubUser getUser(String accessToken) { //通过得到的token获取user OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://api.github.com/user?access_token=" + accessToken) .build(); try { Response response = client.newCall(request).execute(); String string = response.body().string(); //封装 GithubUser githubUser = JSON.parseObject(string, GithubUser.class); //System.out.println(string); return githubUser; } catch (Exception e) { // log.error("getUser error,{}", accessToken, e); } return null; }

 

获取的json内容如下:

 //封装
 JSON.parseObject(string, GithubUser.class);
 

 {
     "login": "Diamondtest",
     "id": 28478049,
     "avatar_url": "https://avatars0.githubusercontent.com/u/28478049?v=3",
     "gravatar_id": "",
     "url": "https://api.github.com/users/Diamondtest",
     "html_url": "https://github.com/Diamondtest",
     "followers_url": "https://api.github.com/users/Diamondtest/followers",
     "following_url": "https://api.github.com/users/Diamondtest/following{/other_user}",
     "gists_url": "https://api.github.com/users/Diamondtest/gists{/gist_id}",
     "starred_url": "https://api.github.com/users/Diamondtest/starred{/owner}{/repo}",
     "subscriptions_url": "https://api.github.com/users/Diamondtest/subscriptions",
     "organizations_url": "https://api.github.com/users/Diamondtest/orgs",
     "repos_url": "https://api.github.com/users/Diamondtest/repos",
     "events_url": "https://api.github.com/users/Diamondtest/events{/privacy}",
     "received_events_url": "https://api.github.com/users/Diamondtest/received_events",
     "type": "User",
     "site_admin": false,
     "name": null,
     "company": null,
     "blog": "",
     "location": null,
     "email": null,
     "hireable": null,
     "bio": null,
     "public_repos": 0,
     "public_gists": 0,
     "followers": 0,
     "following": 0,
     "created_at": "2017-05-06T08:08:09Z&
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇github认证登陆 下一篇Docker下实战zabbix三部曲之二:..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目