日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/13 综合教程 24 生活家
生活随笔 收集整理的這篇文章主要介紹了 使用python+django集成钉钉三方扫码登陆 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用python+django集成釘釘三方掃碼登陸


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

釘釘開發文檔:https://ding-doc.dingtalk.com/doc

釘釘開放平臺:https://open-dev.dingtalk.com/

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

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)

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

最后我們訪問路由就可以得到自己的個人信息。

總結

以上是生活随笔為你收集整理的使用python+django集成钉钉三方扫码登陆的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。