政务钉钉上架以及免登

政务钉钉上架以及免登

Posted by catmai on September 16, 2020

​ 上半年的时候接到需求需要对接浙政钉,结果下半年政务钉钉改版了。应用上架的一些接口有了一些变化,对接测试变得简单了。

​ 总的来说都是好事儿,这边记录一下两次对接有什么差异,方便下次再有类似的需求。

​ 官方文档传送门在这儿:

https://openplatform-portal.dg-work.cn/portal/#/helpdoc?docKey=ksrm&slug=goufls

1.入驻开放平台

​ 其实这步骤不是我来做的,这个得公司申请,公司申请下来之后可以给你分配一个政务钉钉的帐号。登录帐号就可以自建组织内部应用/第三方应用程序。看公司是怎么申请的,有无第三方应用开发权限…这里面具体细节就不太清楚了。

​ 总之,对于开发者来说,先搞到一个政务钉钉的帐号。

2.新建组织内部应用

​ 找到组织内部应用,点击创建,填写相关表单内容即可创建一个内部应用。

photo1

​ 创建完成之后就可以点击详情查看应用详细信息了。

​ 包含大家熟悉的App Key和App Secret(非常重要)photo2

​ 在应用权限页面可以设置应用有关的权限,因为我们只需要一个免登的权限,所以只开启企业内应用免登权限。

photo3

3.开发环境准备

​ 政务钉钉和之前的钉钉所有的接口都换了,需要引用新的sdk。

​ 新的sdk下载地址:https://openplatform-portal.dg-work.cn/portal/#/helpdoc?docKey=gjyzy&slug=st5ct2 下载后添加到项目依赖下就行。

注意:

​ 1.政务钉钉sdk需要java8环境,否则会初始化失败。

​ 2.遇到 org.joda.time.ReadableInstant 报错,请在 pom.xml 中添加如下内容

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.10</version>
</dependency>

4.开发

​ 申请好企业内自建应用之后,可以开始着手代码部分。

​ 步骤和以前大致相同,首先获得一个免登授权码(前端获得),然后获得一个access_token结合免登授权码获得用户信息。

(1)获得免登授权码

​ 新版本获得免登授权码只需要引入js即可,旧版的还需要传递一个corpId参数,新版本的省略了。

新版:

dd.getAuthCode({}).then((result) = >{
    if (result) {
        console.log(JSON.stringify(result, null, null));
        // code: 'hYLK98jkf0m' //string authCode
    }
})

旧版:

dd.ready(function() {
    dd.runtime.permission.requestAuthCode({
        corpId: _config.corpId, // 企业id        
        onSuccess: function (info) {
                  code = info.code // 通过该免登授权码可以获取用户身份
       }});
});

​ 新版JS直接在页面入口引用就可以了,返回的result是一个json对象,内包含了一个code字段即免登授权码。json对象格式如下(如果是正常返回的话)

{
    "expiresIn":"300",
    "code":"xxxxxxxxxxxxxxxxxxx"
}
(2)获得access_token

​ 新版本获得用户信息和access_token的sdk更新了,请求方式较以前更简单了。

​ 政务钉钉的所有接口请求,都需要先初始化一个ExecutableClient对象,然后通过对象构建一个Post或者Get客户端,客户端内添加参数,然后获得返回的结果。

新版:

/**
 * 钉钉2.0获得accessToken
 * @param method 接口名 新版本为:/gettoken.json
 * @param appKey app_id
 * @param appSecret app_secret
 * @return 接口返回的结果
*/
public static String getAccessToken(String method,String appKey,String appSecret) throws Exception{
        logger.info("-------正在获得ACCESSTOKEN----------");
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
    	//钉钉的域名,固定写法
        executableClient.setDomainName("openplatform.dg-work.cn");
        executableClient.setProtocal("https");
        executableClient.init();
        logger.info("初始化完毕");
        GetClient getClient = executableClient.newGetClient(method);
        //设置参数
        getClient.addParameter("appkey", appKey);
        getClient.addParameter("appsecret", appSecret);
        //调用API
        String apiResult = getClient.get();
        return apiResult;
    }

新版的返回值:

{
    "success":true,
    "content":{
        "data":{
            "accessToken":"c139fe44362f41b6b84862ec82ab84d9",
            "expiresIn":"7200"
        },
        "requestId":"df04428415724925400701038d663a",
        "responseMessage":"OK",
        "responseCode":"0",
        "success": true
    }
}

旧版:

//method  https://oapi.dingtalk.com/gettoken
DingTalkClient client = new DefaultDingTalkClient(method);  
OapiGettokenRequest req = new OapiGettokenRequest();  
req.setAppkey(appKey);  
req.setAppsecret(appSecret);  
req.setHttpMethod("GET");  
OapiGettokenResponse rsp = client.execute(req);  
return rsp.getBody();  

(3)获得用户信息

​ 获得用户信息,参数需要一个免登授权码和access_token。

新版:

/**
     * 获得用户信息
     * @param method 接口地址 /rpc/oauth2/dingtalk_app_user.json
     * @param accessToken 上一步拿到的accessToken
     * @param authCode 前端拿到的免登授权码
     * @return
     */
    public static String getUserInfo(String method,String appKey,String appSecret,String accessToken,String authCode) throws Exception{
        logger.info("-------------获取政务钉钉用户-----------");
        ExecutableClient executableClient = ExecutableClient.getInstance();
        executableClient.setAccessKey(appKey);
        executableClient.setSecretKey(appSecret);
        executableClient.setDomainName("openplatform.dg-work.cn");
        executableClient.setProtocal("https");
        executableClient.init();
        //post请求
        PostClient postClient = executableClient.newPostClient(method);
        //设置参数
        postClient.addParameter("access_token", accessToken);
        postClient.addParameter("auth_code", authCode);
        //调用API
        String apiResult = postClient.post();
        return apiResult;
    }

新版的返回值:

{
    "success": true, 
    "content": {
        "responseMessage": "成功", 
        "data": {
            "account": "pishi.hy", 
            "accountId": 93, 
            "clientId": "mozi-developer-center", 
            "employeeCode": "pishi.hy", 
            "lastName": "洪阳", 
            "namespace": "local", 
            "nickNameCn": "洪阳", 
            "openid": "6f1a885a4020f3624b71570b74925d7b", 
            "realmId": 57, 
            "realmName": "绣花针测试租户", 
            "tenantUserId": "57$93"
        }, 
        "responseCode": "0"
    }
}

旧版:

DingTalkClient client = new DefaultDingTalkClient(method);
OapiUserGetuserinfoRequest req = new OapiUserGetuserinfoRequest();
req.setCode(code);
req.setHttpMethod("GET");
OapiUserGetuserinfoResponse rsp = client.execute(req, access_token);
return rsp.getBody();

5.上架

​ 开发完成后,部署至服务器,登录政务钉钉开放平台填写应用服务器信息进行上架。

photo1

​ 填写应用配置信息后,在应用发布页进行发布。然后就可以在政务钉钉工作台中搜索到该应用了。

​ 至此政务钉钉上架流程完毕。