后台开发"/>
Python—实现钉钉后台开发
二、实现钉钉免登流程
免登流程分四步:1、前端获取钉钉免登授权码code;2、后端获取access_token;3、使用授权码code和access_token换取用户userid;4、通过access_token和userid换取用户详情userinfo。
前端获取授权码code。
<script src=".7.13/dingtalk.open.js"></script>
<script src=".10.2/jquery.min.js"></script>
<script type="text/javascript">// 获取当前网页的url:/?corpid=ding46a9582af5b7541b35c2f4657eb6378fvar currentUrl = document.location.toString()$("#url").append(currentUrl)// 解析url中包含的corpIdvar 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.argumentscode = self.get_argument("code", None)# 获取access_tokenAppKey = "ding4itesoimljq9ksmz"AppSecret = "BW8XFsbesRJdOjmt_peYOQBTwVWUkQKONxZ2_2_fXhBQjmgq2Q6tRWrq867l84ht"url = "={0}&appsecret={1}".format(AppKey, AppSecret)resp = requests.get(url)resp = resp.json()access_token = resp["access_token"]# 获取userId url1 = "={0}&code={1}".format(access_token, code)resp1 = requests.get(url1)resp1 = resp1.json()# 获取userInfo url2 = "={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秒,有效期内重复获取返回相同结果,并自动续期。缓不缓存根据自己需要吧。
参考:/,,.html,.html
三、获取钉钉后台人员
四、
.html
.html
.htm?docId=106834&docType=1#/serverapi2/gh60vz
.htm?lwfrom=20180929152232431233#/login
更多推荐
Python—实现钉钉后台开发
发布评论