1. 程式人生 > >釘釘掃碼登錄 java

釘釘掃碼登錄 java

depend 代碼 sem set token jar包 catch exceptio ktr

java獲取掃碼登錄後的釘釘id 從而可以查出員工具體信息 部門具體信息

1.註冊釘釘企業賬號

註冊地址

https://oa.dingtalk.com/register.html

開發文檔地址

https://open-doc.dingtalk.com/microapp/isv

註冊完支行查看corpId corpSecret

創建掃碼登錄使用的app ID、appSecret

技術分享圖片

2.下載釘釘sdk (http://img.alicdn.com/top/i1/LB1l7mXv0knBKNjSZKPXXX6OFXa)把jar包傳到私有maven倉庫

<dependency>
<groupId>com.taobao</groupId>
<artifactId>taobao-sdk-java-auto</artifactId>
<version>20180815</version>
</dependency>

(僅供參考)

3.java代碼使用示例(redis工具類沒貼出來)

需導入包

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;


  private final String SDINGTALKSERVICE="https://oapi.dingtalk.com";
private final String METHOD_GET="GET"; /** * 先從緩存查,再到釘釘查 * @param corpId * @param corpSecret * @return */ public String getToken(String corpId,String corpSecret){ String access_token=RedisUtils.get("ACCESS_TOKEN"); if(StringUtils.isEmpty(access_token)){ access_token
=getTokenFromDing(corpId,corpSecret); } return access_token; } /** * 企業內部開發獲取access_token * @param corpId 企業Id * @param corpSecret 企業應用的憑證密鑰 * @return */ public String getTokenFromDing(String corpId,String corpSecret){ OapiGettokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/gettoken"); OapiGettokenRequest request = new OapiGettokenRequest(); request.setCorpid(corpId); request.setCorpsecret(corpSecret); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } String body=response.getBody(); JSONObject jo = JSONObject.parseObject(body); String errcode=jo.getString("errcode"); String access_token=null; if("0".equals(errcode)){ access_token=(String) jo.get("access_token"); RedisUtils.set("ACCESS_TOKEN", access_token, 3600); } return access_token; } public String getSnsToken(String appId, String appSecret){ String access_token=RedisUtils.get("SNS_ACCESS_TOKEN"); if(StringUtils.isEmpty(access_token)){ access_token=getOpenToken(appId,appSecret); } return access_token; } /** * 獲取釘釘開放應用的ACCESS_TOKEN * @param appId * @param appSecret * @return */ public String getOpenToken(String appId, String appSecret) { OapiSnsGettokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/gettoken"); OapiSnsGettokenRequest request = new OapiSnsGettokenRequest(); request.setAppid(appId); request.setAppsecret(appSecret); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } String body=response.getBody(); JSONObject jo = JSONObject.parseObject(body); String errcode=jo.getString("errcode"); String access_token=null; if("0".equals(errcode)){ access_token=(String) jo.get("access_token"); RedisUtils.set("SNS_ACCESS_TOKEN", access_token, 3600); } return access_token; } /** * 獲取用戶授權的持久授權碼 * @param accessToken * @return */ public String get_persistent_code(String accessToken,String code){ OapiSnsGetPersistentCodeResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/get_persistent_code"); OapiSnsGetPersistentCodeRequest request = new OapiSnsGetPersistentCodeRequest(); request.setTmpAuthCode(code); response= client.execute(request,accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 獲取用戶授權的SNS_TOKEN * @param openId * @param persistentCode * @param accessToken 開放應用的token * @return */ public String get_sns_token(String openId, String persistentCode,String accessToken) { OapiSnsGetSnsTokenResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/get_sns_token"); OapiSnsGetSnsTokenRequest request = new OapiSnsGetSnsTokenRequest(); request.setOpenid(openId); request.setPersistentCode(persistentCode); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getSnsToken(); } /** * 獲取用戶授權的個人信息 * @param snsToken * @return */ public String get_sns_userinfo_unionid(String snsToken) { OapiSnsGetuserinfoResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/getuserinfo"); OapiSnsGetuserinfoRequest request = new OapiSnsGetuserinfoRequest(); request.setSnsToken(snsToken); request.setHttpMethod(METHOD_GET); response = client.execute(request); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 根據unionid獲取userid * @param accessToken * @param unionid * @return */ public String getUseridByUnionid(String accessToken,String unionid) { OapiUserGetUseridByUnionidResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/getUseridByUnionid"); OapiUserGetUseridByUnionidRequest request = new OapiUserGetUseridByUnionidRequest(); request.setUnionid(unionid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 獲取用戶授權的個人信息 * @param accessToken * @param uid * @return */ public String getUserGetOne(String accessToken,String uid) { OapiUserGetResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/get"); OapiUserGetRequest request = new OapiUserGetRequest(); request.setUserid(uid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); } /** * 獲取部門詳情(根據部門id查詢) * @param accessToken * @param deptid * @return */ public String getDepartmentInfo(String accessToken, String deptid) { OapiDepartmentGetResponse response = null; try { DingTalkClient client = new DefaultDingTalkClient(SDINGTALKSERVICE + "/department/get"); OapiDepartmentGetRequest request = new OapiDepartmentGetRequest(); request.setId(deptid); request.setHttpMethod(METHOD_GET); response = client.execute(request, accessToken); } catch (ApiException e) { e.printStackTrace(); } return response.getBody(); }

釘釘開發都非常簡單 按照文檔把要傳的參數傳過去 就可以獲取想要的信息以及信息的增刪改

釘釘掃碼登錄 java