Skip to content

Commit 8314b94

Browse files
committed
获取用户t信息oken , name
1 parent 1540b6b commit 8314b94

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/main/java/life/majiang/community/controller/AuthorizeController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import life.majiang.community.dto.AccessTokenDTO;
8+
import life.majiang.community.dto.GithubUser;
89
import life.majiang.community.provider.GithubProvider;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.stereotype.Controller;
@@ -17,18 +18,20 @@ public class AuthorizeController {
1718
@Autowired // 把spring容器的实例加载到当前上下文
1819
private GithubProvider githubProvider;
1920

20-
2121
@GetMapping("/callback") // 登录成功后返回到登录页
2222
// 接收返回后的参数 code, state
2323
public String callback(@RequestParam(name = "code") String code,
2424
@RequestParam(name = "state") String state) {
2525
AccessTokenDTO accessTokenDTO = new AccessTokenDTO();
2626
accessTokenDTO.setClient_id("639f4c8d6441c6136b44");
27-
accessTokenDTO.setClient_secret("024fb99f36d3ce6764d6e00eb15c9545a284cb9");
27+
accessTokenDTO.setClient_secret("024fb99f36d3ce6764d6e00eb15c9545aa284cb9");
2828
accessTokenDTO.setCode(code);
2929
accessTokenDTO.setRedirect_uri("http://localhost:8080/callback");
3030
accessTokenDTO.setState(state);
3131
githubProvider.getAccess_token(accessTokenDTO);
32+
String accessToken = githubProvider.getAccess_token(accessTokenDTO);
33+
GithubUser user = githubProvider.getUser(accessToken);
34+
System.out.println("用户昵称 >>> " + user.getName()); // 输出 User昵称
3235
return "index";
3336
}
3437

src/main/java/life/majiang/community/provider/GithubProvider.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,28 @@
1212
*/
1313
@Component // 把类初始化到spring容器的上下文中
1414
public class GithubProvider {
15-
// get方法 获取用户token
15+
// post方法 获取用户token
1616
public String getAccess_token(AccessTokenDTO assessTokenDTO) {
1717
MediaType mediaType = MediaType.get("application/json; charset=utf-8");
1818
OkHttpClient client = new OkHttpClient();
1919
RequestBody body = RequestBody.create(JSON.toJSONString(assessTokenDTO), mediaType);
2020
Request request = new Request.Builder()
2121
.url("https://github.com/login/oauth/access_token")
22-
.post(body)
22+
.post(body) // post请求体
2323
.build();
2424

2525
try (Response response = client.newCall(request).execute()) {
2626
String string = response.body().string();
27-
System.out.println(string); // 输出
28-
return string;
29-
} catch (IOException e) {
27+
System.out.println("response响应 >>> " + string); // 输出
28+
// return string; // 返回 string
29+
String[] split = string.split("&"); // 通过"&"分割
30+
String tokenstr = split[0]; // 提取第一个字符串
31+
String token = tokenstr.split("=")[1]; // 通过"="分割
32+
System.out.println("用户token >>> " + token);
33+
return token;
34+
35+
} catch (Exception e) {
36+
e.printStackTrace();
3037
}
3138
return null;
3239
}
@@ -37,6 +44,7 @@ public GithubUser getUser(String accessToken) {
3744
Request request = new Request.Builder()
3845
.url("https://api.github.com/user?access_token=" + accessToken)
3946
.build();
47+
System.out.println("getUser" + accessToken);
4048
try {
4149
Response response = client.newCall(request).execute();
4250
String string = response.body().string();

0 commit comments

Comments
 (0)