handle_token.py
3.7 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/12/15 22:55
# @Author : Shitou
# @FileName: handle_token.py
# @Software: PyCharm
"""
登陆获取token
"""
import requests
from common.handle_config import conf
from jsonpath import jsonpath
# ==================获取登陆token========
# 登陆学校端获取token
class LoginToken:
def login_token():
# 登陆学校端获取token
login_url = conf.get("url", "url_ip") + "/auth/v1/login"
headers = eval(conf.get('url', 'login_token'))
params = {
"way": conf.get('user_data', 'way'),
"password": conf.get('user_data', 'userpassword'),
"type": conf.get('user_data', 'type'),
"username": conf.get('user_data', 'username'),
"schoolId": conf.get('user_data', 'schoolId'),
}
response = requests.request(url=login_url, method="post", json=params, headers=headers)
res = response.json()
# 获取token
token = "Bearer" + " " + jsonpath(res, "$..access_token")[0]
return token
# 登陆小程序学生端获取token
class StudentLoginToken:
def login_token():
# 登陆学生获取token
login_url = conf.get("url", "url_ip") + "/auth/v1/login"
headers = eval(conf.get('url', 'login_token'))
params = {
"way": conf.get("student_user_data", "way"),
"phone": conf.get("student_user_data", "phone"),
"code": conf.get("student_user_data", "code"),
"type": conf.get("student_user_data", "type")
}
response = requests.request(url=login_url, method="post", json=params, headers=headers)
res = response.json()
# 获取token
token = "Bearer" + " " + jsonpath(res, "$..access_token")[0]
return token
# 登陆hr端获取token
class HrLoginToken:
def login_token():
# 登陆hr获取refresh_token
hr_login_url = conf.get("url", "hr_url") + "/uaa/v1/auth/tokens"
params = {
"grant_type": "password",
"tabKey": 1,
"scope": "global_access:tenant_admin,tenant_type:1",
"username": conf.get("hr_user_data", "username"),
"password": conf.get("hr_user_data", "password"),
"smscode": ""
}
refresh_token_response = requests.request(url=hr_login_url, method="post", json=params)
refresh_token_res = refresh_token_response.json()
# 获取token
hr_refresh_token_token = jsonpath(refresh_token_res, "$..refresh_token")[0]
# 登陆hr获取token
# hr_login_url = conf.get("url", "hr_url") + "/uaa/v1/auth/tokens"
params = {
"grant_type": "refresh_token",
"refresh_token": hr_refresh_token_token,
"scope": "global_access:tenant_admin,tenant_type:1,tenant:830460960064933888"
}
response = requests.request(url=hr_login_url, method="post", json=params)
res = response.json()
# 获取token
hr_token = jsonpath(res, "$..access_token")[0] # 获取的到hr登陆的token
yxly_login_url = conf.get("url", "url_ip") + "/auth/v1/login"
yxly_params = {
"way": "tenant",
"tenantToken": "{}".format(hr_token)
}
headers = eval(conf.get('url', 'login_token'))
response2 = requests.request(url=yxly_login_url, method="post", json=yxly_params, headers=headers)
res2 = response2.json()
yxly_token = "Bearer" + " " + jsonpath(res2, "$..access_token")[0] # 带入hr的token获取的到优秀学乐业登陆的token
return yxly_token
if __name__ == '__main__':
HrLoginToken.login_token()
# HrLoginToken.login_token()