-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WRITE_TENANT_ACCESS_TOKEN.py
57 lines (49 loc) · 2.46 KB
/
WRITE_TENANT_ACCESS_TOKEN.py
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
import configparser
from GET_TENANT_ACCESS_TOKEN import GET_TENANT_ACCESS_TOKEN
import argparse
def WRITE_TENANT_ACCESS_TOKEN(app_id=None, app_secret=None, config_file=None):
config = configparser.ConfigParser() # 创建一个ConfigParser对象
if config_file:
config.read(config_file, encoding='utf-8') # 读取指定的配置文件
else:
config.read('feishu-config.ini', encoding='utf-8') # 默认读取名为'feishu-config.ini'的配置文件
# 尝试从app_id和app_secret获取tenant_access_token
try:
tenant_access_token = GET_TENANT_ACCESS_TOKEN(app_id, app_secret, config_file)
# 如果提取的值不存在,将其置为空字符串
if not tenant_access_token:
tenant_access_token = ''
except Exception: # 如果在尝试过程中出现错误,返回None
return None
# 检查配置文件是否存在名为'TOKEN'的section,如果不存在则添加
if 'TOKEN' not in config:
config.add_section('TOKEN')
# 在'TOKEN' section下添加tenant_access_token
config['TOKEN']['tenant_access_token'] = tenant_access_token
# 尝试将新的配置写入到配置文件中
try:
if config_file:
with open(config_file, 'w', encoding='utf-8') as configfile:
config.write(configfile)
else:
with open('feishu-config.ini', 'w', encoding='utf-8') as configfile:
config.write(configfile)
except Exception: # 如果在尝试过程中出现错误,返回None
return None
return tenant_access_token # 如果一切正常,返回提取的值
# 主函数
if __name__ == "__main__":
# 解析命令行参数
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--id', help='app ID')
parser.add_argument('-s', '--secret', help='app secret')
parser.add_argument('--config_file', help='config file path')
args = parser.parse_args()
# 调用WRITE_TENANT_ACCESS_TOKEN函数,将从app_id和app_secret获取的app_access_token写入到配置文件中
result = WRITE_TENANT_ACCESS_TOKEN(args.id, args.secret, args.config_file)
# 检查WRITE_TENANT_ACCESS_TOKEN函数的返回结果
if result is None: # 如果返回None,打印错误信息
print("发生错误,请检查您的输入并再试一次。")
else: # 如果返回的不是None,打印提取的值和成功信息
print(f"app_access_token: {result}")
print("成功写入配置文件")