钉钉扫码登陆获取用户信息

钉钉扫码登陆获取用户信息

准备工作

1.申请开发者的发布应用

1.阅读开发文档

钉钉官方文档中一共写了2种实现方式,我使用的是第二种所以在这里就详细介绍第二种,以供大家参考:
1.在html页面种引入
<script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
写一个HTML的div嵌套二维码

<div id="login_container"></div>

  var obj = DDLogin({
           id:"login_container",
           goto: "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=APPID&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=REDIRECT_URI ",//
           style: "border:none;background-color:#FFFFFF;",//设置样式
           width : "365",//二维码的宽度
           height: "400"//二维码的高度
       });
 });

其中APPID是下面的的APPID,REDIRECT_URI是回调域名,完成后对goto 里面的所有内容进行一个urlencode编码
在这里插入图片描述

var handleMessage = function (event) {
  var origin = event.origin;
  console.log("origin", event.origin);
  if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
    var loginTmpCode = event.data; 
    //构建自己的回调域名其中APPID自己填写REDIRECT_URI自己要跳转的第三方地址
    window.location.href=“https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=APPID&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=REDIRECT_URI&loginTmpCode=”+loginTmpCode;
    console.log("loginTmpCode", loginTmpCode);
  }
};
if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('message', handleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent('onmessage', handleMessage);
}

至此扫码登陆成功

2.获取用信息

钉钉有自己封装的请求我自己原本也有写用的是自己的请求去请求钉钉的地址,代码在下方给出

public static JSONObject httpGet(String url){
       JSONObject jsonrResult = null;
       CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
       HttpGet httpGet = new HttpGet(url);
       try {
           CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
           HttpEntity entity = response.getEntity();
           String result = EntityUtils.toString(entity, "UTF-8");
           jsonrResult = JSON.parseObject(result);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return jsonrResult;
   }

   public static JSONObject httpPost(String url,JSONObject json){
       CloseableHttpClient httpClient = HttpClients.createDefault();
       JSONObject jsonrResult = null;
       HttpPost httpPost = new HttpPost(url);
       try {
           if (null != json){
               StringEntity entity = new StringEntity(json.toString(),"utf-8");
               entity.setContentEncoding("UTF-8");
               entity.setContentType("application/json");
               httpPost.setEntity(entity);
               CloseableHttpResponse response = httpClient.execute(httpPost);
               if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                   String result =EntityUtils.toString(response.getEntity(), "utf-8");
                   System.out.println("result:"+result);
                   jsonrResult = JSONObject.parseObject(result);
               }
           }
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           try {
               httpClient.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return jsonrResult;
   }

下方的第一个请求地址https://oapi.dingtalk.com/sns/gettoken?appid=appid&appsecret=appsecret请求获取的是access_token发送Get请求
第二个请求的地址https://oapi.dingtalk.com/sns/get_persistent_code?access_token="+accesstokenString发送Post请求json 参数
{tmp_auth_code:code} 其中这个code是自己扫码成功后钉钉给予返回的code
第三个请求的地址https://oapi.dingtalk.com/sns/get_sns_token?access_token="+accesstokenString 发送Post请求携带json参数openid 与 persistent_code 其中opdenid与 是上个请求的返回值
第四个请求https://oapi.dingtalk.com/sns/getuserinfo?sns_token=userCode 是第三个请求的返回值

至此应该获取到用户的信息了

//获取access_token
            String access_tokenUrl ="https://oapi.dingtalk.com/sns/gettoken?appid=appid&appsecret=appsecret";
            JSONObject access_token = DDAuthUntil.httpGet(access_tokenUrl);
            String accesstokenString = access_token.getString("access_token");

//获取openid与persistent_code地址
            String geopidPcodeUrl ="https://oapi.dingtalk.com/sns/get_persistent_code?access_token="+accesstokenString;
            String tmp_auth_codeString ="{tmp_auth_code:"+"\""+code+"\""+"}";
            JSONObject tmp_auth_code = JSON.parseObject(tmp_auth_codeString);
            JSONObject sns_tokenResult = DDAuthUntil.httpPost(geopidPcodeUrl, tmp_auth_code);

 //获取sns_token
            String sns_tokenUrl ="https://oapi.dingtalk.com/sns/get_sns_token?access_token="+accesstokenString;
            Map<String,Object> sns_tokenMap = new HashMap<>();
            sns_tokenMap.put("openid",sns_tokenResult.get("openid"));
            sns_tokenMap.put("persistent_code",sns_tokenResult.get("persistent_code"));
            JSONObject openidPcode = new JSONObject(sns_tokenMap);
            JSONObject sns_token = DDAuthUntil.httpPost(sns_tokenUrl, openidPcode);

//获取用户登录认证
            String userCode = sns_token.getString("sns_token");
            System.out.println(userCode);
            String getUserInfoUrl = "https://oapi.dingtalk.com/sns/getuserinfo?sns_token="+userCode;
            JSONObject user = DDAuthUntil.httpGet(getUserInfoUrl);

appid和appsecret在这里找到
在这里插入图片描述
参考:https://ding-doc.dingtalk.com/doc#/serverapi2/kymkv6 (钉钉官方文档) https://www.cnblogs.com/vicky1018/p/9086171.html(钉钉扫码登陆)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值