Nail Code Scan Login Website
- Objective
Due to the separation of the company back and forth, here is mainly about the background of the implementation of logic and process, the front-end related to a bit. Front-end we are using the QR code embedded in our web page.
The process is as follows:
1. Login nail Backstage Create an enterprise app
2. Obtain Access_token according to AppID and App_secret
- The Access_token is valid for 2 hours and can be cached on its own.
/** * 获取access token 有效期2 个小时,有效期获取,值不变,时间延长 * @return */ public static DdResult getAccessToken(String appid, String secret){ String requestUrl = "https://oapi.dingtalk.com/sns/gettoken?appid=APPID&appsecret=APPSECRET"; requestUrl = requestUrl.replace("APPID", appid).replace("APPSECRET", secret); String accessTokenResult = HttpClientUtil.doGet(requestUrl); log.info("------->> >> -------->> request ding ding access token result:{}", accessTokenResult); if (StringUtils.isNotBlank(accessTokenResult)) { return JSON.parseObject(accessTokenResult, new TypeReference<DdResult>(){}); } return new DdResult(); }
3. Get Persistent Code Persistent_code
- Based on the code returned from the front end and the Access_token obtained in the previous step get Persistent_code, the persistent code has no expiration time temporarily.
/** * 根据code与accessToken获取用户的持久授权码 * @param code 临时码,只能使用一次 * @param accessToken 有效期,2个小时 * @return */ public static DdResult getPersistentCode(String code, String accessToken){ String requestUrl = "https://oapi.dingtalk.com/sns/get_persistent_code?access_token=" + accessToken; HashMap<String, String> params = Maps.newHashMap(); params.put("tmp_auth_code", code); String accessTokenResult = HttpClientUtil.doPostJson(requestUrl, JSON.toJSONString(params)); log.info("------->> >> -------->> ding ding persistent token result: {}", accessTokenResult); return JSON.parseObject(accessTokenResult, DdResult.class); }
4. Get the user authorization code Sns_token
- Based on the Openid,persistent_code obtained in the previous step, get the sns_token with the Access_token obtained in 2 steps
/** * 获取sns token * @param accessToken * @param openId * @param persistentCode * @return */ public static DdResult getSnsToken(String accessToken, String openId, String persistentCode) { HashMap<String, String> params = Maps.newHashMap(); params.put("openid", openId); params.put("persistent_code", persistentCode); String snsTokenRequestUrl = "https://oapi.dingtalk.com/sns/get_sns_token?access_token=" + accessToken; String result = HttpClientUtil.doPostJson(snsTokenRequestUrl, JSON.toJSONString(params)); log.info("------->> >> -------->> request ding ding sns token result:{}", result); return JSON.parseObject(result, new TypeReference<DdResult>(){}); }
5. Get user Information
- Gets the user information based on the Sns_token obtained in the previous step.
public static DdResult getUserInfo(String snsToken) { String userInfoRequestUrl = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token=" + snsToken; String result = HttpClientUtil.doGet(userInfoRequestUrl); log.info("------->> >> -------->> >> request ding ding sns token result:{}", result); if (StringUtils.isBlank(result)) { return null; } return JSON.parseObject(result, new TypeReference<DdResult>() {}); }
To get the user's information, you can do the login operation.
- Reference:
1. https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.n3ywjK&treeId=168&articleId=104882 &doctype=1
2.76681381
Nail Code Scan Login Web site