因工作需求,需要对接浙政钉,故记录下自己的对接过程
1. 注册专有钉钉ISV并且创建应用
ps: 注册申请提交后,需要等待他们的工作人员审核,通过后会发账号密码到手机上。
根据下方链接,建立基础数据并创建应用
openplatform-portal.dg-work.cn/portal/#/he…
ps: 1、创建应用这步比较坑,不能直接用超管创建应用。得添加一个开发者角色的账号去创建应用,不然应用开发里可能看不到应用。
2、千万记得选择应用使用范围。 工作台发布后,如果没配置这个,还是看不到应用的,见下图。
获取appkey和appsecrt等参数
2. 下载java sdk
ps:官网的链接下载不了,我是在群里下载的。钉钉群号:34302232。
顺便丢个阿里云盘地址吧。www.aliyundrive.com/s/mun5JPiGU… 提取码: t07o
3. cv工程师拿手好戏
为了方便后来人屎山接手采用了mvn本地引入jar包的方式
<dependency>
<groupId>com.alibaba.xxpt</groupId>
<artifactId>zwdd</artifactId>
<version>1.2.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/zwdd-sdk-java-1.2.0.jar</systemPath>
</dependency>
public static void main(String[] args) {
ExecutableClient executableClient =ExecutableClient.getInstance();
executableClient.setAccessKey("appkey");
executableClient.setSecretKey("appsecrt");
executableClient.setDomainName("openplatform.dg-work.cn");
executableClient.setProtocal("https");
executableClient.init();
//executableClient要单例,并且使用前要初始化,只需要初始化一次
String api = "/gettoken.json";
GetClient getClient = executableClient.newGetClient(api);
//设置参数
getClient.addParameter("appkey", "appkey");
getClient.addParameter("appsecret", "appsecret");
//调用API
String apiResult = getClient.get();
System.out.println(apiResult);
}
一些报错的问题
User not authorized to operate on the specified resource. 检查应用的服务端权限是否正常
各种NoClassDefFoundException
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
因为我是boot项目,所以上面有两个包我就没指定版本号。
4. 部署一个前端到服务端
下面的代码我是没试过。虽然我懂点前端,但是我就不试,诶就是玩儿。
npm install gdt-jsapi
import dd from 'gdt-jsapi';
dd.getAuthCode({}).then(res =>{
console.log(res)
}).
catch(err =>{})
5. 通过前端调用jsapi获取的免登码,去请求免登接口
我是直接去页面上复制的免登码,通过qq发到电脑上的。
6. 展现cv技术的时候到了
// 构造请求对象
GetClient getClient = executableClient.newGetClient("/rpc/oauth2/dingtalk_app_user.json");
getClient.addParameter("access_token", accessToken);
getClient.addParameter("auth_code", "4562c2a4e4974d759fe5b6d8eff9a4000cd05501");
//调用API
String apiResult = getClient.get();
System.out.println(apiResult);
7. 最后我封装了一下这个发起请求的代码
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.example.demo03.zzd.ResponseDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
public class ApiTest {
private static ExecutableClient executableClient;
private static final String get_token = "/gettoken.json";
private static final String user_info = "/rpc/oauth2/dingtalk_app_user.json";
private static final String appkey = "appkey";
private static final String appsecret = "appsecret";
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
executableClient = ExecutableClient.getInstance();
executableClient.setAccessKey(appkey);
executableClient.setSecretKey(appsecret);
executableClient.setDomainName("openplatform.dg-work.cn");
executableClient.setProtocal("https");
executableClient.init();
//executableClient要单例,并且使用前要初始化,只需要初始化一次
}
public static void main(String[] args) throws Exception {
// 参数
HashMap<String, Object> reqMap = new HashMap<>();
reqMap.put("appkey", appkey);
reqMap.put("appsecret", appsecret);
// 调用API
ResponseDto accessTokenRes = sendRequest(get_token, reqMap);
String accessToken = null;
if(accessTokenRes.getSuccess()) {
accessToken = (String) accessTokenRes.getContent().getData().get("accessToken");
// 参数
HashMap<String, Object> reqMap2 = new HashMap<>();
reqMap2.put("access_token", accessToken);
reqMap2.put("auth_code", "前端提供的免登码");
ResponseDto request = sendRequest(user_info, reqMap2);
}
}
private static ResponseDto sendRequest(String uri, Map map) throws JsonProcessingException {
// 打印请求体
System.out.println(objectMapper.writeValueAsString(map));
// 构造请求对象
GetClient getClient = executableClient.newGetClient(uri);
// 设置参数
if(map != null) {
for (Object key : map.keySet()) {
Object value = map.get(key);
// 如果值为null就不传了
if(value != null) {
getClient.addParameter(key.toString(), value.toString());
}
}
}
//调用API
String apiResult = getClient.get();
// 打印返回体
System.out.println(apiResult);
// 转为Dto返回
return objectMapper.readValue(apiResult, ResponseDto.class);
}
}
ResponseDto类的代码
import java.util.Map;
public class ResponseDto {
public class ContentDto {
private Map data;
private Boolean success;
private String requestId;
private String responseMessage;
private String responseCode;
private String bizErrorCode;
public Map getData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String getResponseMessage() {
return responseMessage;
}
public void setResponseMessage(String responseMessage) {
this.responseMessage = responseMessage;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
public String getBizErrorCode() {
return bizErrorCode;
}
public void setBizErrorCode(String bizErrorCode) {
this.bizErrorCode = bizErrorCode;
}
}
private Boolean success;
private ContentDto content;
private String bizErrorCode;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public ContentDto getContent() {
return content;
}
public void setContent(ContentDto content) {
this.content = content;
}
public String getBizErrorCode() {
return bizErrorCode;
}
public void setBizErrorCode(String bizErrorCode) {
this.bizErrorCode = bizErrorCode;
}
}