钉钉企业注册:点击跳转

官方文档:点击跳转

悦哥钉钉登录博客:点击查看

钉钉登录

1.注册钉钉企业

2.登录企业后台管理

3.选择应用开发

4.移动接入应用->登录
顶顶登录1

5.创建扫码登录应用授权
顶顶登录2
顶顶登录3

6.获取创建以后企业应用的appid,appsecret,回调域名
顶顶登录4

代码示例

1
2
3
4
5
6
7
8
9
10
urls:path('ding_url/',ding_login),
#登录顶顶
def ding_login(request):
#钉钉应用的appid
appid = 'dingoab7edavvvtdcwqcra'
# 钉钉应用的回调地址
redirect_uri = 'http://127.0.0.1:8000/callback/'
#重定向到
return redirect('https://oapi.dingtalk.com/connect/qrconnect?appid='
+appid+'&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+redirect_uri)

7.钉钉会将code返回到回调网址中
顶顶登录5
因为钉钉只有java和php的sdk,所以使用hmac加密自己写sdk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
urls:path('callback/',ding_back),
#构造钉钉对调方法。
def ding_back(request):
#获取code
code = request.GET.get('code',None)
print('code是:%s'%code)

#构造时间戳
t = time.time()
timestamp = str(int(round(t*1000)))
print('时间:',timestamp)

#URL签名参数说明
#钉钉的appSecret
appSecret = '8F3q2UmU-2ljILNJAtodsnFaLllpMEioNWI5GRtnoz5OyubMYbiTxlVC3yrPmHiE'
#构造签名
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?'
'accessKey=dingoab7edavvvtdcwqcra&timestamp='+timestamp+
'&signature='+urllib.parse.quote(signature.decode('utf-8')),headers=headers,data=json.dumps(payload))

res_dict = json.loads(res.text)
print(res_dict)
username = str(res_dict['user_info']['nick'])
user = User.objects.filter(username=username).first()
if user:
#数据库有该用户名,直接登录
user.username = username
print('已经登陆过,用户名',username)
else:
#没有登录,注册
user = User(username=username)
user.save()
print('首次注册,用户名',username)
return redirect('http://localhost:8080/?username='+username)
# return HttpResponse(res.text)

顶顶登录6

评论





载入天数...载入时分秒...

Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use WZH as theme, total visits times