zoukankan      html  css  js  c++  java
  • 使用python+django集成钉钉三方扫码登陆

    使用python+django集成钉钉三方扫码登陆


    参考博客:https://v3u.cn/a_id_124

    钉钉开发文档:https://ding-doc.dingtalk.com/doc#/serverapi2/kymkv6

    钉钉开放平台:https://open-dev.dingtalk.com/#/loginMan

    (~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

    1. 进入钉钉开放平台---》点击左下角 ----》移动接入应用----》登陆----》点击创建扫码登陆应用授权。

    (~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

    2. 创建一个网站应用,其中有用的信息是appid,appsecret,还有回调网址 。

    (~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

    3. 根据官方文档构建登陆扫码url。

    #构造钉钉登录url
    def ding_url(request):
        appid = 'dingoaukgkwqknzjvamdqh'	#替换成自己的appid
        redirect_uri = 'http://localhost:8000/dingding_back/'	#替换成自己的回调路由
    
        return redirect('https://oapi.dingtalk.com/connect/qrconnect?appid='+appid+'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+redirect_uri)
    

    然后访问http://localhost:7878/ding_url,就可以进行扫码

    (~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

    4. 最后我们编写回调url。将时间戳,秘钥进行hmac加密

    import time
    import hmac
    import base64
    from hashlib import sha256
    import urllib
    import json
    
    #构造钉钉回调方法
    def ding_back(request):
    
        #获取code
        code = request.GET.get("code")
    
        t = time.time()
        #时间戳
        timestamp = str((int(round(t * 1000))))
    	#替换成自己的appSecret
        appSecret ='ly-AzMKMmCKQP3geaILT_An32kEfKO3HeOtApy5CgKwjytevVZC0WYsT2gxMB160'
        #构造签名
        signature = base64.b64encode(hmac.new(appSecret.encode('utf-8'),timestamp.encode('utf-8'), digestmod=sha256).digest())
        #请求接口,换取钉钉用户名
        payload = {'tmp_auth_code':code}
        headers = {'Content-Type': 'application/json'}
        res = requests.post('https://oapi.dingtalk.com/sns/getuserinfo_bycode?signature='+urllib.parse.quote(signature.decode("utf-8"))+"&timestamp="+timestamp+"&accessKey=dingoaukgkwqknzjvamdqh",data=json.dumps(payload),headers=headers)	#accessKey替换成自己的appid
    
        res_dict = json.loads(res.text)
        print(res_dict)
        return HttpResponse(res.text)
    

    (~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

    最后我们访问路由就可以得到自己的个人信息。

  • 相关阅读:
    正则表达式
    linux中的三种时间
    用户,用户组
    inode与block
    linux文件属性
    linux的启动过程
    linux的重要子目录
    mail邮箱
    spark原理
    spark部署
  • 原文地址:https://www.cnblogs.com/tjw-bk/p/14164513.html
Copyright © 2011-2022 走看看