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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM