Python—实现钉钉后台开发

二、实现钉钉免登流程

免登流程分四步:1、前端获取钉钉免登授权码code;2、后端获取access_token;3、使用授权码code和access_token换取用户userid;4、通过access_token和userid换取用户详情userinfo。

前端获取授权码code。

<script src="https://g.alicdn.com/dingding/dingtalk-jsapi/2.7.13/dingtalk.open.js"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    // 获取当前网页的url:http://ding-web.lnexin.cn/?corpid=ding46a9582af5b7541b35c2f4657eb6378f
    var currentUrl = document.location.toString()
    $("#url").append(currentUrl)
    // 解析url中包含的corpId
    var corpId = currentUrl.split("corpid=")[1];
    $("#corpId").append(corpId)
	
    // 钉钉sdk初始化:dd.ready参数为回调函数,在环境准备就绪时触发,jsapi的调用需要保证在该回调函数触发后调用,否则无效。
    dd.ready(function () {
        // 使用SDK 获取免登授权码
        dd.runtime.permission.requestAuthCode({
            corpId: 'dingovyrjosjwioznxqn',
            onSuccess: function (result) {
                alert(JSON.stringify(result));
                var code = result.code;
                //$.get("http://49.232.56.68:8006/get/user",'code='+info.code,function(response){
                $.get("http://49.232.56.68:8006/get/user?code=" + code, function (response) {
                    var response = JSON.parse(response)
                    // 下面的业务根据自己的需求来写
                    if (response.user) {
                        for (item in response.user) {
                            $("#result").append("<li>\t[user 属性] " + item + " : " + response.user[item] + "</li>")
                        }
                    }
                });
            },
            onFail: function(err) {
                alert('fail: ' + JSON.stringify(err));   //处理失败的情况
            },
        });
    });
	
    // 如果没有走ready方法的话会走error方法。
    // dd.error(function(error){
    dd.error((error) => {
        alert('dd error: ' + JSON.stringify(error));
        alert(`dd error: ${JSON.stringify(error)}`);
    });	
</script>

后台获取access_token,然后获取userId,最后换取userInfo。

class AdminUser(BaseHandler):
    def get(self):
        info = self.request.arguments
        code = self.get_argument("code", None)
		
        # 获取access_token
        AppKey = "ding4itesoimljq9ksmz"
        AppSecret = "BW8XFsbesRJdOjmt_peYOQBTwVWUkQKONxZ2_2_fXhBQjmgq2Q6tRWrq867l84ht"
        url = "https://oapi.dingtalk.com/gettoken?appkey={0}&appsecret={1}".format(AppKey, AppSecret)
        resp = requests.get(url)
        resp = resp.json()
        access_token = resp["access_token"]
	
        # 获取userId	
        url1 = "https://oapi.dingtalk.com/user/getuserinfo?access_token={0}&code={1}".format(access_token, code)
        resp1 = requests.get(url1)
        resp1 = resp1.json()
	
        # 获取userInfo	
        url2 = "https://oapi.dingtalk.com/user/get?access_token={0}&userid={1}".format(access_token, resp1["userid"])
        resp2 = requests.get(url2)
        resp2 = resp2.json()
        return self.write(json.dumps({"status": "success", "userinfo": resp2}))

授权码code是每次请求都不一样,单次请求的数据5分钟有效,所以没必要缓存,直接用一次调一次。 正常情况下access_token有效期为7200秒,有效期内重复获取返回相同结果,并自动续期。缓不缓存根据自己需要吧。
参考:https://hasfun.cn/2019/03/17/dingdingdev/https://www.jianshu.com/p/b02eea9f09ffhttps://www.anji66.net/article/id/143.htmlhttps://www.cnblogs.com/zuiwoshanlin/p/9481539.html

三、获取钉钉后台人员

https://hacpai.com/article/1569062348311

四、

https://blog.csdn.net/weixin_42336574/article/details/95485622

https://blog.csdn.net/weixin_42042680/article/details/86661387

https://blog.csdn.net/qq_26608423/article/details/90510923

https://blog.csdn.net/Jason847/article/details/75007140

https://www.cnblogs.com/applerosa/p/11509512.html

https://dingtalk-sdk.readthedocs.io/zh_CN/latest/client/api/calendar.html

https://ding-doc.dingtalk.com/doc.htm?docId=106834&docType=1#/serverapi2/gh60vz

https://oa.dingtalk.com/index.htm?lwfrom=20180929152232431233#/login

https://gitee.com/liu993083028/ding-server

posted @ 2019-11-26 23:41  刘_love_田  阅读(4724)  评论(0编辑  收藏  举报