实现钉钉登录

成为钉钉开发人员

1.在手机版的钉钉中通讯录中创建一个企业应用
2.登陆的时候选择你创建的组织
3.在此应用中创建h5微应用

在这里插入图片描述

在这里插入图片描述
登录分享:里面的地址就是你钉钉登录完成之后跳转的地址
权限管理要勾选两个:获取用户的:个人手机号信息,通讯录个人信息读权限
应用信息:里面的AgentId,AppKey,AppSecret要用到

登录的地址

在这里插入图片描述
登录的地址需要拼接出来

https://login.dingtalk.com/oauth2/auth?
redirect_uri=http%3A%2F%2Fwww.aaaaa.com%2Fauth
# www.aaaaa.com=>>>与上面的分享的地址一致
# %3A >>是 :
# %2F >>是 /
&response_type=code		 // 不变
&client_id=dingxxxxxxx   //应用的AppKey 
&scope=openid   //此处的openId保持不变
&state=dddd		// 不变	
&prompt=consent	//不变

# 将上面及部分拼接成一个钉钉登录的网址
https://login.dingtalk.com/oauth2/auth?redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2Fmain&response_type=code&client_id=dingfdqpgjlwqkosnqnq&scope=openid&state=dddd&prompt=consent
# 生成的地址,可以直接在前端使用,跳转

上面的地址可以直接在前端跳转

传递authCode到后端进行身份验证

在这里插入图片描述

在这里插入图片描述

#  window.location.href = 跳转的地址
# 跳转之后是上面两个图的样子
# 上面第二个地址栏里的参数就是我们需要的登录信息
#前端调转到main页面里面,在该页面接收参数authCode
this.code = this.$route.query.authCode
# 接受参数 传递到后端 进行身份验证
this.$axios.get('user/login/'+this.code)
接受参数校验用户信息

1.根据 authCode 获取用户的token(post)
2.根据 token 获取用户的信息 (get)

在这里插入图片描述
在这里插入图片描述

class DingView(Resource):
    def get(self,code):
        # 发post请求
        url1 =  'https://api.dingtalk.com/v1.0/oauth2/userAccessToken'
        # 参数
        data = {
            "clientId": "dingy6ghdl3tfpxil9op",
            "clientSecret": "pBkgFOGKXPhUw2OHrG6zueZ0iKwCekzAYObnzbE-6Y4-K7eWgKbMSEw7z-XM2k4f",
            "code": code,# 返回的参数
            "grantType": "authorization_code"
        }
        # 发送post请求返回的有code,可以获取用户accessToken
        res = requests.post(url1,json=data) 
        token_data = json.loads(res.text) # 字符串,转化为json
        token = token_data['accessToken']
        print(token)
        # 发get请求
        url2 = 'https://api.dingtalk.com/v1.0/contact/users/me'
        # 参数
        headers ={
            'x-acs-dingtalk-access-token':token
        }
        rs = requests.get(url2,headers=headers)
        # 拿到用户信息,进入身份验证,进入正轨
        user_info_data = json.loads(rs.text)
        print(user_info_data)



api.add_resource(DingView,'/dingding/<code>')  # code > 就是前端传递过来的authCode
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值