使⽤django钉钉第三⽅扫码登录
钉钉作为阿⾥旗下的⼀款免费移动通讯软件,受众体越来越多,这⾥我们使⽤Django来集成⼀下钉钉的三⽅账号登录,⾸先注册钉钉开发平台:
在移动应⽤中选择登录
创建⼀个⽹站应⽤,其中有⽤的信息是 appid , appsecret ,还有回调⽹址
随后,查看官⽅⽂档,查看如何构造登录url:
这⾥我们⽤django视图来操作
#构造钉钉登录url
def ding_url(request):
appid = 'dingoaukgkwqknzjvamdqh'
redirect_uri = 'localhost:8000/dingding_back/'
django登录注册功能return redirect('oapi.dingtalk/connect/qrconnect?appid='+appid+'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+redirect_uri)
然后访问,就可以进⾏扫码
随后,钉钉会将code返回到回调⽹址中,查看官⽅⽂档,只有 java 和 php 的 sdk,并没有python的
SDK请求⽰例(JAVA):
DefaultDingTalkClient  client = new DefaultDingTalkClient("oapi.dingtalk/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest();
req.setTmpAuthCode("4a2c5695b78738d495f47b5fee9160cd");
OapiSnsGetuserinfoBycodeResponse response = ute(req,"yourAppId","yourAppSecret");
SDK请求⽰例(PHP):
include "TopSdk.php";
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiSnsGetuserinfoBycodeRequest;
$req->setTmpAuthCode("4a2c5695b78738d495f47b5fee9160cd");
$resp=$c->executeWithAccessKey($req, "oapi.dingtalk/sns/getuserinfo_bycode","yourAppId","yourAppSecret");
var_dump($resp)
python 逻辑很简单,将时间戳,秘钥进⾏hmac加密即可
# 构造钉钉回调⽅法
# 导包:
import hmac
import base64
from hashlib import sha256
import urllib
import json
def ding_url(request):
appid = 'dingoadckiwhdceemaxrza',
redirect_uri = '127.0.0.1:8000/ding_url'
# 获取code
code = ("code")
t = time.time()
# 时间戳
timestamp = str((int(round(t * 1000))))
# 密钥
appSecret = 'Uym9ecJfNF4vlQ9-8wAGl2J10GvyZNKJqiXvcuf0blp7ERTpWrif8IwWk_AGI1j-'
# 构造签名
signature = base64.b64encode(
# 请求接⼝,换取钉钉⽤户名
payload = {'tmp_auth_code': code}
headers = {'Content-Type': 'application/json'}
# parse(alt+回车第⼆个导包)
res = requests.post('oapi.dingtalk/sns/getuserinfo_bycode?signature=' + urllib.parse.quote(
signature.decode("utf-8")) + "×tamp=" + timestamp + "&accessKey=dingoadckiwhdceemaxrza",
data=json.dumps(payload), headers=headers)
res_dict = json.)
print(res_dict)
访问⼀下,可以显⽰出钉钉的⽤户名
vue与django结合钉钉第三放登录
//vue登录页⾯内点击事件
//钉钉登录
dingding() {
//拼接钉钉url
let url = "oapi.dingtalk/connect/qrconnect?appid=dingoadckiwhdceemaxrza&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=127.0.0.1:8000/ding_url"
//进⾏站外跳转
window.location.href = url;
},
django视图页⾯:
# 导包:
import hmac
import base64
from hashlib import sha256
import urllib
import json
def ding_url(request):
appid = 'dingoadckiwhdceemaxrza',
redirect_uri = '127.0.0.1:8000/ding_url'
# 'https: // oapi.dingtalk / connect / qrconnect?appid =dingoamo0tezhk5hsckrrk & response_type = code & scope = snsapi_login & state = STATE & redirect_uri =localhost:8000/dingding_back/'    # 获取code
code = ("code")
t = time.time()
# 时间戳
timestamp = str((int(round(t * 1000))))
appSecret = 'Uym9ecJfNF4vlQ9-8wAGl2J10GvyZNKJqiXvcuf0blp7ERTpWrif8IwWk_AGI1j-'
# 构造签名
signature = base64.b64encode(
# 请求接⼝,换取钉钉⽤户名
payload = {'tmp_auth_code': code}
headers = {'Content-Type': 'application/json'}
# parse(alt+回车第⼆个导包)
res = requests.post('oapi.dingtalk/sns/getuserinfo_bycode?signature=' + urllib.parse.quote(
signature.decode("utf-8")) + "×tamp=" + timestamp + "&accessKey=dingoadckiwhdceemaxrza",
data=json.dumps(payload), headers=headers)
res_dict = json.)
#判断是否为第⼀次登录
user = models.User.objects.filter(username=str(res_dict['user_info']['nick'])).first()
sina_name = ''
user_id = ''
# 代表曾经登陆
if user:
sina_name = user.username
user_id = user.id
else:
# ⾸次登录建⽴账号保存到数据库
models.User(username=str(res_dict['user_info']['nick']), password=make_password(''), type=0).save()
print('已创建钉钉账户')
# 查询⽤户
user = models.User.objects.filter(username=str(res_dict['user_info']['nick'])).first()
sina_name = res_dict['user_info']['nick']
user_id = user.id
# 进⾏跳转
return redirect("localhost:8080?sina_name=" + str(sina_name) + "&uid=" + str(user_id))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。