• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

第三方平台使用钉钉账号登录

武飞扬头像
大可不必872
帮助1

思路

学新通

 1.成为钉钉开放平台开发者

想要通过钉钉账号实现第三方登录,首先要成为钉钉开放平台开发者,从而获取APPIDappSecret

2.前端点击钉钉登录按钮发送登录请求到后端(携带一个code(临时授权码))

3.后端根据前端发送的code和Appid,appSecret构造跳转链接

  1.  
    appid = current_app.config.get('DINGDING_APPID')
  2.  
    appSecret = current_app.config.get('DINGDING_SECRET')
  3.  
    timestamp = str(int(time.time() * 1000))
  4.  
    base_url = "https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature="
  5.  
    signature = base64.b64encode(
  6.  
    hmac.new(appSecret.encode('utf-8'), timestamp.encode('utf-8'),
  7.  
    digestmod='sha256').digest())
  8.  
    url = base_url urllib.parse.quote(signature.decode()) '&timestamp=' timestamp
  9.  
    '&accessKey=' appid

4.使用钉钉账号登录

5.根据临时授权码获取用户信息

  1.  
    data = json.dumps({'tmp_auth_code': code})
  2.  
    try:
  3.  
    resp = requests.post(url, data, headers={'Content-Type': 'application/json'})
  4.  
    print('resp>>>', resp.json())
  5.  
    except Exception as e:
  6.  
    return {'code': 500, 'message': '获取用户标识信息失败'}
  7.  
    user_info = resp.json()
  8.  
    if user_info['errcode'] != 0:
  9.  
    return {'code': 500, 'message': '获取用户标识信息失败'}
  10.  
    user_info_dict = user_info['user_info']
  11.  
    return user_info_dict

返回实例

  1.  
    {
  2.  
    "errcode":0,
  3.  
    "user_info":{
  4.  
    "nick":"名字",
  5.  
    "unionid":"dingdkjjojoixxxx",
  6.  
    "openid":"dingsdsqwlklklxxxx",
  7.  
    "main_org_auth_high_level":true
  8.  
    },
  9.  
    "errmsg":"ok"
  10.  
    }

6.判断钉钉账号是否与该用户绑定

user_info = self.get_dingding_user(code)
openid = user_info['openid']
if openid:
    oauth_user = OAuthUserModel.query.filter_by(oauth_id=openid).first()
    if oauth_user:
        user = User.query.filter_by(uid=oauth_user.user).first()
        data = {
            'username': user.username,
            'uid': user.uid
        }
        token = _generate_token(data)
        return {'code': 200, 'account': user.username, 'uid': user.uid, 'token': token}
    else:
        return {'code': 500, 'message': '没用绑定用户,请重新绑定', 'uid': openid}

若已绑定则登录成功,若没有绑定需要先绑定在登录 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhecjhja
系列文章
更多 icon
同类精品
更多 icon
继续加载