java实现钉钉扫码登录_钉钉扫码登录 java

本文介绍如何使用Java实现钉钉扫码登录。首先从缓存中获取access_token,若为空则通过钉钉API获取。接着获取钉钉开放应用的ACCESS_TOKEN,然后获取用户授权的持久授权码、SNS_TOKEN以及个人信息。最后,根据unionid获取用户ID并获取用户详细信息。
摘要由CSDN通过智能技术生成

private final String SDINGTALKSERVICE="https://oapi.dingtalk.com";private final String METHOD_GET="GET";/*** 先从缓存查,再到钉钉查

*@paramcorpId

*@paramcorpSecret

*@return

*/

publicString getToken(String corpId,String corpSecret){

String access_token=RedisUtils.get("ACCESS_TOKEN");if(StringUtils.isEmpty(access_token)){

access_token=getTokenFromDing(corpId,corpSecret);

}returnaccess_token;

}/*** 企业内部开发获取access_token

*@paramcorpId 企业Id

*@paramcorpSecret 企业应用的凭证密钥

*@return

*/

publicString getTokenFromDing(String corpId,String corpSecret){

OapiGettokenResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/gettoken");

OapiGettokenRequest request= newOapiGettokenRequest();

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);

}returnaccess_token;

}publicString getSnsToken(String appId, String appSecret){

String access_token=RedisUtils.get("SNS_ACCESS_TOKEN");if(StringUtils.isEmpty(access_token)){

access_token=getOpenToken(appId,appSecret);}returnaccess_token;

}/*** 获取钉钉开放应用的ACCESS_TOKEN

*@paramappId

*@paramappSecret

*@return

*/

publicString getOpenToken(String appId, String appSecret) {

OapiSnsGettokenResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/gettoken");

OapiSnsGettokenRequest request= newOapiSnsGettokenRequest();

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);

}returnaccess_token;

}/*** 获取用户授权的持久授权码

*@paramaccessToken

*@return

*/

publicString get_persistent_code(String accessToken,String code){

OapiSnsGetPersistentCodeResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/get_persistent_code");

OapiSnsGetPersistentCodeRequest request= newOapiSnsGetPersistentCodeRequest();

request.setTmpAuthCode(code);

response=client.execute(request,accessToken);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getBody();

}/*** 获取用户授权的SNS_TOKEN

*@paramopenId

*@parampersistentCode

*@paramaccessToken 开放应用的token

*@return

*/

publicString get_sns_token(String openId, String persistentCode,String accessToken) {

OapiSnsGetSnsTokenResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/get_sns_token");

OapiSnsGetSnsTokenRequest request= newOapiSnsGetSnsTokenRequest();

request.setOpenid(openId);

request.setPersistentCode(persistentCode);

response=client.execute(request, accessToken);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getSnsToken();

}/*** 获取用户授权的个人信息

*@paramsnsToken

*@return

*/

publicString get_sns_userinfo_unionid(String snsToken) {

OapiSnsGetuserinfoResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/getuserinfo");

OapiSnsGetuserinfoRequest request= newOapiSnsGetuserinfoRequest();

request.setSnsToken(snsToken);

request.setHttpMethod(METHOD_GET);

response=client.execute(request);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getBody();

}/*** 根据unionid获取userid

*@paramaccessToken

*@paramunionid

*@return

*/

publicString getUseridByUnionid(String accessToken,String unionid) {

OapiUserGetUseridByUnionidResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/getUseridByUnionid");

OapiUserGetUseridByUnionidRequest request= newOapiUserGetUseridByUnionidRequest();

request.setUnionid(unionid);

request.setHttpMethod(METHOD_GET);

response=client.execute(request, accessToken);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getBody();

}/*** 获取用户授权的个人信息

*@paramaccessToken

*@paramuid

*@return

*/

publicString getUserGetOne(String accessToken,String uid) {

OapiUserGetResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/get");

OapiUserGetRequest request= newOapiUserGetRequest();

request.setUserid(uid);

request.setHttpMethod(METHOD_GET);

response=client.execute(request, accessToken);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getBody();

}/*** 获取部门详情(根据部门id查询)

*@paramaccessToken

*@paramdeptid

*@return

*/

publicString getDepartmentInfo(String accessToken, String deptid) {

OapiDepartmentGetResponse response= null;try{

DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/department/get");

OapiDepartmentGetRequest request= newOapiDepartmentGetRequest();

request.setId(deptid);

request.setHttpMethod(METHOD_GET);

response=client.execute(request, accessToken);

}catch(ApiException e) {

e.printStackTrace();

}returnresponse.getBody();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值