"钉钉三方登录"


## django
```
//
# 构造钉钉回调方法
def ding_back(request):
# 获取code
code = request.GET.get("code")

t = time.time()
# 时间戳
timestamp = str((int(round(t * 1000))))
appSecret = '3ahHLFxM8C7-PRce6IfnALdIFo2IgLGPCAFbRNUSSwJ22aGCwBUKTebwrdtnseu4'
# 构造签名
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=dingoaodxh6mz0zfsejad8",
data=json.dumps(payload), headers=headers)

res_dict = json.loads(res.text)
print(res_dict['user_info']['nick'])
username = res_dict['user_info']['nick']
user = Users.objects.filter(username=username).first()
if user:
ding_id = user.username
user_did = user.id
else:
# 手动创建账号
user = Users(username=username, password='')
user.save()
ding_id = username
# 查询刚才入库的新账号id
user = Users.objects.filter(username=username).first()
user_did = user.id

return redirect("http://127.0.0.1:8080?ding_id=" + ding_id + "&uid=" + str(user_did))
```
## VUE
```
//钉钉登录
dingding: function () {

let appid = 'dingoaodxh6mz0zfsejad8';
let redirect_uri = 'http://localhost:8000/ding_back/';

//进行跳转 站外跳转
window.location.href = 'https://oapi.dingtalk.com/connect/qrconnect?appid=' + appid + '&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + redirect_uri

//进行跳转 站外跳转
window.location.href = url
},
```
posted @ 2020-06-18 18:55  LSC_Progressing  阅读(115)  评论(0)    收藏  举报