直接上代码
url写完整的:
"http://localhost:8083/oauth/" + "authorize"
+ "?client_id=" + clientId
+ "&response_type=" + "code"
+ "&scope=" + scope
+ "&redirect_uri=" + webServerRedirectUri;
这里需要发送三次http请求,有的应该两次就行,两次的我没写明白,用的时HuTool的工具
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.11</version>
</dependency>
private String getcode(String url, String username, String password) {
HashMap<String, Object> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("username",username);
stringStringHashMap.put("password",password);
HttpRequest post = HttpRequest.post(url);
HttpResponse execute = post.execute();
String location1 = execute.header("Location");
HttpResponse execute2 = HttpRequest.post(location1)
.form(stringStringHashMap).execute();
HttpRequest post3 = HttpRequest.post(url);
HttpResponse execute3 = post3.execute();
String location3 = execute3.header("Location");
String code = location3.substring(location3.indexOf("=") + 1, location3.length());
System.out.println(code);
return code;
}
下面时生成token的
hutool的body不用解析,直接就能用
private String getToken(String urlToken) {
HttpResponse execute = HttpRequest.post(urlToken)
.execute();
String body = execute.body();
return body;
}