DingTalk internal small program free login

DingTalk automatic login requires two things:

         One is access_token (access token), and the other is code (login-free authorization code).

         (The authorization codes for enterprise applications and personal applications can be obtained through this JSAPI.)

dd.getAuthCode({
    success:function(res){
        /*{
            authCode: 'hYLK98jkf0m' //string authCode
        }*/
    },
    fail:function(err){
    }
});


authCode authorization code, which is valid for 5 minutes and can only be used once.
After obtaining it, the userid of the user will be obtained, and then the detailed information of the user can be obtained through access_token and userid

The acquisition of access_token also requires two things

         One is AppKey, one is AppSecret

(AgentId: When creating an application, the system will automatically generate an AgentId, which can be used in scenarios such as sending enterprise session messages. AppKey: When creating an application, the system will automatically
    assign a pair of AppKey and AppSecret. The AppKey is the only one in the application development process. AppSecret
    : AppSecret is generated together with the above AppKey, use AppKey and AppSecret to exchange for access_token.)

So the process is to get code and access_token, then get userid through these two, and then get user details through userid and access_token

 onLoad() {
    dd.getAuthCode({
      success: (res) => {
        var code = res.authCode; //免登授权码
        dd.httpRequest({
          //根据appKey和appsecret来获取Token
          url: "https://oapi.dingtalk.com/gettoken?appkey=???&appsecret=???",
          success: function (res) {
 
            console.log("res:", res);
 
            var access_token = res.data.access_token;
 
            console.log("access_token:", access_token);
 
            dd.httpRequest({
              url: 'https://oapi.dingtalk.com/user/getuserinfo?access_token=' + access_token + '&code=' + code,
              success: function (res) {
 
                console.log("获取得到的用户信息:", res);
 
                dd.httpRequest({
                  url: 'https://oapi.dingtalk.com/user/get?access_token=' + access_token + '&userid=' + res.data.userid,
                  success: function (res) {
 
                     console.log("根据访问Token和用户id得到用户详细信息:", res)
                     console.log("用户Id:", res.data.userid);
                     console.log("用户名", res.data.name);
                     console.log("电话", res.data.mobile);
                     console.log("errmsg:", res.data.errmsg);
                     console.log("角色权限:", res.data.roles);
 
                  },
                });
              },
            });
          },
        });
      },
      fail: (err) =>{
        dd.alert({content: JSON.stringify(err)})
      },
 
    });
 
  }

Guess you like

Origin blog.csdn.net/weixin_60842212/article/details/124745487