5
5
import os
6
6
7
7
def GET_USER_ACCESS_TOKEN (login_code ):
8
+ print ("Starting the process of getting the user access token." )
9
+
8
10
# 配置文件路径
9
11
config_path = 'feishu-config.ini'
10
12
11
13
# 确保配置文件存在
12
14
if not os .path .exists (config_path ):
15
+ print ("The configuration file does not exist. Creating a new one." )
13
16
with open (config_path , 'w' ) as f :
14
17
pass
15
18
@@ -19,6 +22,7 @@ def GET_USER_ACCESS_TOKEN(login_code):
19
22
20
23
# 确保 TOKEN 部分存在
21
24
if not config .has_section ('TOKEN' ):
25
+ print ("The TOKEN section does not exist in the configuration file. Adding it." )
22
26
config .add_section ('TOKEN' )
23
27
24
28
# 从配置文件获取参数
@@ -41,28 +45,33 @@ def GET_USER_ACCESS_TOKEN(login_code):
41
45
"code" : login_code
42
46
}
43
47
48
+ print ("Sending the request to get the user access token." )
44
49
# 发起请求
45
50
response = requests .post (url , headers = headers , data = json .dumps (payload ))
46
51
response_json = response .json ()
47
52
48
- # 检查响应状态码
53
+ # 检查响应状态码
49
54
if response .status_code != 200 :
50
55
print (f"HTTP Error: { response_json } " )
51
56
return None
52
57
53
- # 检查响应体中的 code
58
+ # 检查响应体中的 code
54
59
if response_json .get ('code' ) != 0 :
55
60
print (f"Response Error: { response_json } " )
56
61
return None
57
62
58
-
59
63
# 更新配置文件
60
64
if 'data' in response_json and 'access_token' in response_json ['data' ]:
61
65
config .set ('TOKEN' , 'user_access_token' , response_json ['data' ]['access_token' ])
66
+ # 如果存在 refresh_token,也保存到配置文件
67
+ if 'refresh_token' in response_json ['data' ]:
68
+ print ("Refresh token found. Updating the configuration file." )
69
+ config .set ('TOKEN' , 'refresh_token' , response_json ['data' ]['refresh_token' ])
62
70
with open (config_path , 'w' , encoding = 'utf-8' ) as configfile :
63
71
config .write (configfile )
64
72
65
- return response_json .get ('data' , {}).get ('access_token' )
73
+ print ("Finished getting the user access token." )
74
+ return response_json .get ('data' , {}).get ('access_token' ), response_json .get ('data' , {}).get ('refresh_token' )
66
75
67
76
68
77
def GET_USER_ACCESS_TOKEN_CMD ():
@@ -82,10 +91,11 @@ def GET_USER_ACCESS_TOKEN_CMD():
82
91
raise ValueError ("login_code is required either in command line argument or in the configuration file." )
83
92
84
93
# 调用 GET_USER_ACCESS_TOKEN 函数,获取 user_access_token
85
- user_access_token = GET_USER_ACCESS_TOKEN (login_code )
94
+ user_access_token , refresh_token = GET_USER_ACCESS_TOKEN (login_code )
86
95
87
96
# 打印结果
88
97
print (f'user_access_token: { user_access_token } ' )
98
+ print (f'refresh_token: { refresh_token } ' )
89
99
90
100
91
101
# 主函数
0 commit comments