钉钉开放平台
填写相关资料,这里会有appid和appSecret:
下载Java版本的SDK 扫码登陆1.png
这里面给出了一个连接:
https://oapi.dingtalk.com/connect/qrconnect?appid=APPID&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=REDIRECT_URI
连接相关参数的解释:
连接参数解释.png
访问这个连接会产生一个二维码,当你扫码点击登陆的时候,就会跳转到连接的回调地址。代码如下:
@ResponseBody
@RequestMapping("/dingdingLogin")
public Object dingdingLogin() {
String time = String.valueOf(System.currentTimeMillis());//产生一个当前的毫秒
StringBuilder stringBuilder = new StringBuilder();
String result="";
stringBuilder
.append("https://oapi.dingtalk.com/connect/qrconnect?appid=")
.append(DingTalkConstant.APP_ID)//APP_ID
.append("&response_type=")
.append("code")//code
.append("&scope=")
.append("snsapi_login")//snsapi_login
.append("&state=")
.append(time)
.append("&redirect_uri=")
.append(DingTalkConstant.CALL_BACK_URL);//回调地址
try {
result = stringBuilder.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
/**钉钉注册接口的回调地址
public static final String CALL_BACK_URL = "http://192.168.0.0:8080/userAuths/dingdingCallback"; */
/**
* 钉钉回调验证
*/
@RequestMapping(value="/dingdingCallback", produces="text/html; charset=utf-8")
public Object getUserInfo(HttpServletRequest request, HttpServletResponse response, Model model, String code, String state) {
Result result = userAuthsService.getDingLogin(code);
return null;
}
@Service
public class DingDingServiceImpl implements DingDingService {
/**
* 通过扫描二维码返回的code值,得到用户相关信息
*/
public Result getDingLogin(String code) {
try{
//获取accesstoken,返回accessToken
String accessToken = getAccesstoken();
//获取用户授权的持久授权码,返回accessToken
JSONObject json = getPersistentCode(accessToken, code);
String openId = "";
String persistentCode = "";
if(null!=json){
openId = json.getString("openid");
persistentCode = json.getString("persistent_code");
}
//获取用户授权的SNS_TOKEN,返回snsToken
String snsToken = getSnsToken(accessToken, openId, persistentCode);
//获取用户的昵称和dingId
JSONObject userJson = getUserName(snsToken);
if (Integer.parseInt(userJson.get("errcode").toString()) == 0) {
JSONObject jsonUser = userJson.getJSONObject("user_info");
String nick = jsonUser.getString("nick");
String dingId = jsonUser.getString("dingId");
String openid = jsonUser.getString("openid");
String errmsg = jsonUser.getString("errmsg");
String unionid = jsonUser.getString("unionid");
}
/*//获取用户unionid
String unionId = getUnionId(snsToken);
//根据unionid获取用户userId,需要企业ID和企业秘钥
String appAccessToken = getAppAccesstoken();
String userId = getUserId(appAccessToken, unionId);
if (StringUtils.isEmpty(unionId)) {
return null;
}
//获取用户详细数据
userData = getUserData(appAccessToken, userId);*/
}
/**
* 获取accesstoken
*/
public String getAccesstoken() throws OApiException {
String url = "https://oapi.dingtalk.com/sns/gettoken?appid="
+DingTalkConstant.APP_ID
+"&appsecret="
+DingTalkConstant.APP_SECRET;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
String accessToken = json.getString("access_token");
return accessToken;
}
}
return "";
}
/**
* 获取用户授权的持久授权码
*/
public JSONObject getPersistentCode(String accessToken,String code) throws OApiException {
String url = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token="
+ accessToken;
JSONObject jsonData = new JSONObject();
jsonData.put("tmp_auth_code", code);
JSONObject json = ossHttpPostUtil(url, jsonData);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
return json;
}
}
return null;
}
/**
* 获取用户授权的SNS_TOKEN
*/
public String getSnsToken(String accesstoken, String openid, String persistent_code) throws OApiException {
String url = "https://oapi.dingtalk.com/sns/get_sns_token?access_token="
+accesstoken;
HttpPost httpPost = new HttpPost(url);
JSONObject jsonData = new JSONObject();
jsonData.put("openid", openid);
jsonData.put("persistent_code", persistent_code);
JSONObject json = ossHttpPostUtil(url, jsonData);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
String snsToken = json.getString("sns_token");
return snsToken;
}
}
return null;
}
/**
* 获取用户昵称和snsToken
*/
public JSONObject getUserName(String snsToken) throws OApiException {
String url = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token="
+snsToken;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
return json;
}
return null;
}
/**
* 获取用户unionid
*/
public String getUnionId(String snsToken) throws OApiException {
String url = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token="
+snsToken;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
JSONObject jsonUser = json.getJSONObject("user_info");
String unionid = jsonUser.getString("unionid");
return unionid;
}
}
return "";
}
/**
* 获取appAccesstoken信息(corpid(企业ID)和corpsecret(企业密钥))
*/
public String getAppAccesstoken() throws OApiException {
String url = "https://oapi.dingtalk.com/gettoken?corpid="
+DingTalkConstant.DING_TALK_CORP_ID
+"&corpsecret="
+DingTalkConstant.DING_TALK_CORP_SECRET;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
String appAccessToken = json.getString("access_token");
return appAccessToken;
}
}
return "";
}
/**
* 获取用户详细数据
*/
public JSONObject getUserData(String accessToken, String userId) throws OApiException {
String url = "https://oapi.dingtalk.com/user/get?access_token="+accessToken+"&userid="+userId;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
return json;
}
}
return null;
}
/**
* 根据unionid获取用户userId
*/
public String getUserId(String accessToken, String unionId) throws OApiException {
String url = "https://oapi.dingtalk.com/user/getUseridByUnionid?unionid="+unionId+"&access_token="+accessToken;
JSONObject json = HttpHelper.httpGet(url);
if(null!=json){
if (Integer.parseInt(json.get("errcode").toString()) == 0) {
String userId = json.getString("userid");
return userId;
}
}
return "";
}
private JSONObject ossHttpPostUtil(String url, JSONObject json){
HttpPost httpPost = new HttpPost(url);
HttpEntity httpEntity = null;
httpEntity = new StringEntity(json.toString(), "UTF-8");
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = null;
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpClient httpClient = httpClientBuilder.build();
try {
httpResponse = httpClient.execute(httpPost);
} catch (Exception e) {
}
StringBuilder entityStringBuilder = new StringBuilder();
//得到httpResponse的状态响应码
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
//得到httpResponse的实体数据
HttpEntity httpEntity2 = httpResponse.getEntity();
JSONObject jsonObject = null;
if (httpEntity2 != null) {
try {
return jsonObject = jsonObject.parseObject(EntityUtils.toString(httpEntity2));
} catch (Exception e) {
}
}
}
return null;
}
}
网友评论