Skip to content

Commit b400586

Browse files
committed
钉钉获取普通用户登录信息
1 parent ee89867 commit b400586

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

SpringBootDemo/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@
101101
<dependency>
102102
<groupId>org.apache.httpcomponents</groupId>
103103
<artifactId>httpclient</artifactId>
104-
<version>4.5</version>
105104
</dependency>
106105

107106
<dependency>
108107
<groupId>org.apache.httpcomponents</groupId>
109108
<artifactId>httpmime</artifactId>
110-
<version>4.5</version>
111109
</dependency>
112110

113111

SpringBootDemo/src/main/java/com/xiaour/spring/boot/controller/TestCtrl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ public String userinfo(String code){
121121
}
122122
return "";
123123
}
124+
125+
@RequestMapping("/snsUser")
126+
public String snsUser(String code){
127+
try {
128+
String jsonStr=AuthHelper.snsTokenUser(code);
129+
System.err.println(jsonStr);
130+
return jsonStr;
131+
} catch (Exception e) {
132+
e.printStackTrace();
133+
}
134+
return "";
135+
}
124136
}

SpringBootDemo/src/main/java/com/xiaour/spring/boot/utils/HttpHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public static JSONObject httpPost(String url, Object data) throws OApiException
8686
setSocketTimeout(2000).setConnectTimeout(2000).build();
8787
httpPost.setConfig(requestConfig);
8888
httpPost.addHeader("Content-Type", "application/json");
89-
9089
try {
9190
StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), "utf-8");
9291
httpPost.setEntity(requestEntity);

SpringBootDemo/src/main/java/com/xiaour/spring/boot/utils/aes/AuthHelper.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,57 @@ public static String getSsoToken() throws OApiException {
233233

234234
}
235235

236+
237+
public static String snsTokenUser(String userCode) throws OApiException {
238+
String accessToken;
239+
String url = "https://oapi.dingtalk.com/sns/gettoken?appid="+Env.SNS_APP_ID+"&appsecret="+Env.SNS_APP_SECRET;
240+
JSONObject response = HttpHelper.httpGet(url);
241+
242+
if (response.containsKey("access_token")) {
243+
accessToken = response.getString("access_token");
244+
JSONObject json1=getPersistentCode(accessToken,userCode);
245+
246+
JSONObject json2=getSnsToken(accessToken,json1.getString("openid"),json1.getString("persistent_code"));
247+
248+
String userInfo=getSnsUserinfo(json2.getString("sns_token"));
249+
return userInfo;
250+
} else {
251+
throw new OApiResultException("Sso_token");
252+
}
253+
254+
}
255+
256+
257+
private static JSONObject getPersistentCode(String accessToken,String userCode) throws OApiException{
258+
String snsUrl = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token="+accessToken;
259+
260+
JSONObject data=new JSONObject();
261+
data.put("tmp_auth_code", userCode);
262+
//String jsonStr="{\"tmp_auth_code\": \""+userCode+"\"}";
263+
JSONObject snsResult = HttpHelper.httpPost(snsUrl, data);
264+
return snsResult;
265+
}
266+
267+
private static JSONObject getSnsToken(String accessToken,String openid,String persistentCode) throws OApiException{
268+
String snsUrl = "https://oapi.dingtalk.com/sns/get_sns_token?access_token="+accessToken;
269+
JSONObject data=new JSONObject();
270+
data.put("openid", openid);
271+
data.put("persistent_code", persistentCode);
272+
273+
//String jsonStr="{\"openid\": \""+openid+"\",\"persistent_code\": \""+persistentCode+"\"}";
274+
JSONObject snsResult = HttpHelper.httpPost(snsUrl, data);
275+
return snsResult;
276+
}
277+
278+
private static String getSnsUserinfo(String snsToken) throws OApiException{
279+
String snsUrl = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token="+snsToken;
280+
JSONObject snsResult = HttpHelper.httpGet(snsUrl);
281+
return snsResult.toJSONString();
282+
}
283+
284+
285+
286+
236287
public static String getUserinfo(String code) throws OApiException {
237288
String url = "https://oapi.dingtalk.com/user/getuserinfo?access_token="+getAccessToken()+"&code="+code;
238289
JSONObject response = HttpHelper.httpGet(url);

SpringBootDemo/src/main/java/com/xiaour/spring/boot/utils/aes/Env.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ public class Env {
2222
public static final String TOKEN = "";
2323
public static final String ENCODING_AES_KEY = "";
2424

25+
26+
27+
public static final String SNS_APP_ID = "dingoates0filrnnxqbetd";
28+
public static final String SNS_APP_SECRET = "KQgORLyXYMcnfE1C6iXVSv1epdfiH_xiWARV6lApPgcAFOTr1SLzAFpr3ErBkpBQ";
29+
30+
2531
}

0 commit comments

Comments
 (0)