在第一篇文章里说明了钉钉应用开发配置和后台参数配置,接下来就是代码调用接口。jar包版本:taobao-sdk-java-auto_1479188381469-20211227.jar。首先通过钉钉配置的应用首页,前端判断是否钉钉调用然后请求后端,前端代码略,后端收到请求然后发送调用钉钉链接:
StringBuilder builder =new StringBuilder();
// 构造钉钉OAuth2登录授权地址
builder.append("https://oapi.dingtalk.com/connect/oauth2/sns_authorize");
// 授权通过/拒绝后回调地址。
// 注意 需要与注册应用时登记的域名保持一致。后台回调函数地址,前面域名保持一致就行
String redirectUri ="http://sgtest.vaiwan.com/cz-park/sys/thirdLogin/oauth2/dingtalk/callback";
builder.append("?redirect_uri=").append(URLEncoder.encode(redirectUri, "UTF-8"));
// 固定值为code。
// 授权通过后返回code。
builder.append("&response_type=code");
// 步骤一中创建的应用详情中获取。
// 企业内部应用:client_id为应用的AppKey。
builder.append("&appid=").append(config.getClientId());
// 授权范围,授权页面显示的授权信息以应用注册时配置的为准。
// snsapi_auth:授权后可获得用户userid
builder.append("&scope=snsapi_auth");
// 跟随authCode原样返回。
builder.append("&state=").append(state);
url = builder.toString();
}else {
return "不支持的source";
}
log.info("oauth2 login url:" + url);
response.sendRedirect(url);
上面redirect以后需要开发相关的回调函数
获取token,代码如下:
public static StringgetUserAccessToken(String clientId, String clientSecret) {
JSONObject params =new JSONObject();
params.put("appKey", clientId);
params.put("appSecret", clientSecret);
String url ="https://api.dingtalk.com/v1.0/oauth2/accessToken";
JSONObject response = HttpUtil.sendPost(url, params.toJSONString());
if (response !=null) {
String accessToken = response.getString("accessToken");
if (accessToken !=null && accessToken.length() >0) {
return accessToken;
}
}
return null;
}
//根据code和token 还有secret和appkey获取user信息,在这里我只需要userId
public String getUserId(String code, String accessToken,ThirdAppTypeItemVo itemVo) {
DefaultDingTalkClient client2 =new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest reqBycodeRequest =new OapiSnsGetuserinfoBycodeRequest();
reqBycodeRequest.setTmpAuthCode(code);
OapiSnsGetuserinfoBycodeResponse bycodeResponse =null;
try {
bycodeResponse = client2.execute(reqBycodeRequest, itemVo.getClientId(), itemVo.getClientSecret());
}catch (ApiException e) {
e.printStackTrace();
}
// 根据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 =null;
try {
oapiUserGetbyunionidResponse = clientDingTalkClient.execute(reqGetbyunionidRequest, accessToken);
}catch (ApiException e) {
e.printStackTrace();
}
// 根据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.setLanguage("zh_CN");
OapiV2UserGetResponse rspGetResponse =null;
try {
rspGetResponse = clientDingTalkClient2.execute(reqGetRequest, accessToken);
}catch (ApiException e) {
e.printStackTrace();
}
System.out.println(rspGetResponse.getBody());
return rspGetResponse.getResult().getUserid();
}
以上是怎么钉钉怎么免密登录第三方的与钉钉交互的部分。剩下的就是自己业务处理。