栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

钉钉第三方登录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

钉钉第三方登录

思路流程:

 

1.具体实现步骤:
  1. 在前端页面点击第三方的图标,进行页面的跳转
  2. 跳转页面有钉钉或者是QQ登录的二维码,进行登录或注册
  3. 返回给后端服务器用户的信息生成token,然后传参给前端浏览器
  4. 浏览器保存token信息,下次扫码时验证token直接登录第三方应用
2.先是获取钉钉开放平台应用的appid,appSecret,还有时间戳
        # 构建三个参数的值,构建跳转连接地址
        appid = current_app.config.get('DINGDING_APP_ID')
        appSecret = current_app.config.get('DINGDING_APP_SECRET')
        timestamp = str(int(time.time() * 1000))
        # .digest()获取hash对象的值
        signature = base64.b64encode(
            hmac.new(appSecret.encode('utf-8'), timestamp.encode('utf-8'), digestmod="sha256").digest())

        base_url = "https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature="
        # 拼接url
        url = base_url + urllib.parse.quote(
            signature.decode('utf-8')) + "×tamp=" + timestamp + "&accessKey=" + appid
3.获取用户信息
        data = json.dumps({'tmp_auth_code': code})
        try:
            resp = requests.post(url, data, headers={'Content-Type': 'application/json'})
            print("resp>>>", resp.json())
            user_info = resp.json()
        except Exception as e:
            # 根据code 获取用户信息失败
            return {'code': 500, "message": 'post 获取钉钉用户信息失败'}
        if user_info['errcode'] != 0:
            # 获取当前登录的而用户信息失败
            return {'code': 500, "message": 'code获取钉钉用户信息失败'}
        user_dict = user_info['user_info']
        return user_dict
返回格式:
{
        "errcode":0,
        "user_info":{
                "nick":"名字",
                "unionid":"dingdkjjojoixxxx",
                "openid":"dingsdsqwlklklxxxx",
                "main_org_auth_high_level":true
        },
        "errmsg":"ok"

}

4.判断是否绑定本地用户

 data = json.dumps({'tmp_auth_code': code})
        try:
            resp = requests.post(url, data, headers={'Content-Type': 'application/json'})
            print("resp>>>", resp.json())
            user_info = resp.json()
        except Exception as e:
            # 根据code 获取用户信息失败
            return {'code': 500, "message": 'post 获取钉钉用户信息失败'}
        if user_info['errcode'] != 0:
            # 获取当前登录的而用户信息失败
            return {'code': 500, "message": 'code获取钉钉用户信息失败'}
        user_dict = user_info['user_info']
        return user_dict


如没有绑定则进行绑定

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号