You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.7 KiB
79 lines
3.7 KiB
package com.epmet.controller;
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
import com.dingtalk.api.DingTalkClient;
|
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
|
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
|
|
import com.dingtalk.api.request.OapiUserGetbyunionidRequest;
|
|
import com.dingtalk.api.request.OapiV2UserGetRequest;
|
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
|
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
|
|
import com.dingtalk.api.response.OapiUserGetbyunionidResponse;
|
|
import com.dingtalk.api.response.OapiV2UserGetResponse;
|
|
import com.epmet.commons.tools.utils.Result;
|
|
import com.taobao.api.ApiException;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
/**
|
|
* 免登第三方网站
|
|
*
|
|
* @author openapi@dingtalk
|
|
*/
|
|
@RestController("dingtalk")
|
|
public class DingdingLoginController {
|
|
|
|
/**
|
|
* 获取授权用户的个人信息 openapi@dingtalk
|
|
*
|
|
* @return
|
|
* @throws Exception ServiceResult<Map<String,Object>> 2020-11-4
|
|
*/
|
|
@RequestMapping(value = "/auth", method = RequestMethod.GET)
|
|
public Result<String> getDdScan(@RequestParam("code") String code) throws Exception {
|
|
// 获取access_token,注意正式代码要有异常流处理
|
|
String access_token = this.getToken();
|
|
|
|
// 通过临时授权码获取授权用户的个人信息
|
|
DefaultDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
|
|
OapiSnsGetuserinfoBycodeRequest reqBycodeRequest = new OapiSnsGetuserinfoBycodeRequest();
|
|
|
|
reqBycodeRequest.setTmpAuthCode(code);
|
|
OapiSnsGetuserinfoBycodeResponse bycodeResponse = client2.execute(reqBycodeRequest, "yourAppId", "yourAppSecret");
|
|
|
|
// 根据unionid获取userid
|
|
String unionid = bycodeResponse.getUserInfo().getUnionid();
|
|
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
|
|
OapiUserGetbyunionidRequest reqGetbyunionidRequest = new OapiUserGetbyunionidRequest();
|
|
reqGetbyunionidRequest.setUnionid(unionid);
|
|
OapiUserGetbyunionidResponse oapiUserGetbyunionidResponse = clientDingTalkClient.execute(reqGetbyunionidRequest, access_token);
|
|
|
|
// 根据userId获取用户信息
|
|
String userid = oapiUserGetbyunionidResponse.getResult().getUserid();
|
|
DingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient(
|
|
"https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
OapiV2UserGetRequest reqGetRequest = new OapiV2UserGetRequest();
|
|
reqGetRequest.setUserid(userid);
|
|
//reqGetRequest.setLang("zh_CN");
|
|
OapiV2UserGetResponse rspGetResponse = clientDingTalkClient2.execute(reqGetRequest, access_token);
|
|
System.out.println(rspGetResponse.getBody());
|
|
return new Result<String>().ok(rspGetResponse.getBody());
|
|
}
|
|
private String getToken() throws RuntimeException {
|
|
try {
|
|
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
|
request.setAppkey("dingiopfbtn8mktfoaf0");
|
|
request.setAppsecret("RcmHIoP5KFLZSM5wzpYhvCKMMKEzLoWPtqu3OqOEBD6myg4IT8oVw4AwvRkKYKJz");
|
|
request.setHttpMethod("GET");
|
|
OapiGettokenResponse response = client.execute(request);
|
|
return response.getAccessToken();
|
|
} catch (ApiException e) {
|
|
throw new RuntimeException();
|
|
}
|
|
|
|
}
|
|
}
|
|
|