钉钉内部企业应用免登录 php js
生活随笔
收集整理的這篇文章主要介紹了
钉钉内部企业应用免登录 php js
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我這里采用前端獲取免登錄授權碼,發給后端計算得到登錄后的釘釘信息。
index.html
dd.ready(function() {dd.runtime.permission.requestAuthCode({corpId: "你的corpid",onSuccess: function(info) {code = info.code//引入axios,ajax獲取登錄后的內容axios.get('login.php?code='+code).then(function (response) {//自行處理response}).catch(function (error) {alert('something wrong')});},onFail : function(err) {alert(err) }})});login.php
<?php header('Content-type: application/json'); function request_by_curl($remote_server,$postStr) {$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $remote_server);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 線下環境不用開啟curl證書驗證, 未調通情況可嘗試添加該代碼curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);if($postStr != ''){curl_setopt($ch, CURLOPT_POST, 1);//post提交方式curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);}$data = curl_exec($ch);curl_close($ch);return $data; }//獲取access_token//修改相應的appsecret appkey $webhook = "https://oapi.dingtalk.com/gettoken?appkey=dingniusxv**********j&appsecret=xges_02fDh4gWjJABh4_7P*************gXLH8N6UL2W_1QX"; $result = request_by_curl($webhook,''); echo $result;//用access_token、免登授權碼獲取userid $token = json_decode($result)->{'access_token'}; $code = $_GET['code']; $postStr = "{\"code\": \"{$code}\"}"; $webhook = "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token={$token}"; $result = request_by_curl($webhook,$postStr); echo $result;//用userid、token取用戶信息 $userid = json_decode($result)->{'result'}->{'userid'}; $postStr = "{\"userid\": \"{$userid}\"}";$webhook = "https://oapi.dingtalk.com/topapi/v2/user/get?access_token={$token}"; $result = request_by_curl($webhook,$postStr);echo $result; ?>這兩個頁面上傳好,通過釘釘工作臺訪問內部應用后,就可以獲得當前賬號的信息了。
對照官方文檔處理時遇到一個坑:
axios Error: Network Error{"errcode":40078,"errmsg":"不存在的臨時授權碼"}反復看了文檔后,覺得沒有任何問題。后來發現是前端的鍋,前端只能獲取免登授權碼,后端進行access_token,userid,人員信息等。
參考文檔:
https://developers.dingtalk.com/document/app/enterprise-internal-application-logon-free?spm=ding_open_doc.document.0.0.36582f17ShLZ7v#topic-2021731
按照這四步思路做就可以。
總結
以上是生活随笔為你收集整理的钉钉内部企业应用免登录 php js的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 技术分享:font-spider(字蛛)
- 下一篇: 动态规划算法php,php算法学习之动态