Skip to content

Commit f069fc2

Browse files
committed
3.3.0
1 parent 280e288 commit f069fc2

File tree

5 files changed

+136
-2
lines changed

5 files changed

+136
-2
lines changed

.github/workflows/python-publish.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_RECORD_ID/from .GET_RECORD_ID/g' {} \;
3636
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_RECORD/from .GET_RECORD/g' {} \;
3737
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_TABLE_ID/from .GET_TABLE_ID/g' {} \;
38+
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_TENANT_ACCESS_TOKEN/from .GET_TENANT_ACCESS_TOKEN/g' {} \;
3839
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_USER_ACCESS_TOKEN/from .GET_USER_ACCESS_TOKEN/g' {} \;
3940
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from GET_VIEW_ID/from .GET_VIEW_ID/g' {} \;
4041
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from LIST_FIELDS/from .LIST_FIELDS/g' {} \;
@@ -48,6 +49,7 @@ jobs:
4849
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from WRITE_LOGIN_CODE/from .WRITE_LOGIN_CODE/g' {} \;
4950
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from WRITE_RECORD_ID/from .WRITE_RECORD_ID/g' {} \;
5051
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from WRITE_TABLE_ID/from .WRITE_TABLE_ID/g' {} \;
52+
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from WRITE_TENANT_ACCESS_TOKEN/from .WRITE_TENANT_ACCESS_TOKEN/g' {} \;
5153
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from WRITE_VIEW_ID/from .WRITE_VIEW_ID/g' {} \;
5254
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from CREATE_FIELD/from .CREATE_FIELD/g' {} \;
5355
find FeishuBitableAPI -name "*.py" -exec sed -i 's/from CREATE_TABLE/from .CREATE_TABLE/g' {} \;

FeishuBitableAPI.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from GET_RECORD_ID import GET_RECORD_ID
66
from GET_RECORD import GET_RECORD
77
from GET_TABLE_ID import GET_TABLE_ID
8+
from GET_TENANT_ACCESS_TOKEN import GET_TENANT_ACCESS_TOKEN
89
from GET_USER_ACCESS_TOKEN import GET_USER_ACCESS_TOKEN
910
from GET_VIEW_ID import GET_VIEW_ID
1011

@@ -21,6 +22,7 @@
2122
from WRITE_LOGIN_CODE import WRITE_LOGIN_CODE
2223
from WRITE_RECORD_ID import WRITE_RECORD_ID
2324
from WRITE_TABLE_ID import WRITE_TABLE_ID
25+
from WRITE_TENANT_ACCESS_TOKEN import WRITE_TENANT_ACCESS_TOKEN
2426
from WRITE_VIEW_ID import WRITE_VIEW_ID
2527

2628
from CREATE_FIELD import CREATE_FIELD
@@ -91,6 +93,10 @@ def GET_RECORD(self, app_token=None, table_id=None, record_id=None, config_file=
9193
def GET_TABLE_ID(self, name="数据表", app_token=None, user_access_token=None, page_size=None, page_token=None, config_file=None):
9294
return GET_TABLE_ID(name, app_token, user_access_token, page_size, page_token, config_file)
9395

96+
#GET_TENANT_ACCESS_TOKEN
97+
def GET_TENANT_ACCESS_TOKEN(self, app_id=None, app_secret=None, config_file=None):
98+
return GET_TENANT_ACCESS_TOKEN(app_id, app_secret, config_file)
99+
94100
#GET_USER_ACCESS_TOKEN
95101
def GET_USER_ACCESS_TOKEN(self, login_code=None, app_access_token=None, config_file=None):
96102
return GET_USER_ACCESS_TOKEN(login_code, app_access_token, config_file)
@@ -142,7 +148,11 @@ def WRITE_RECORD_ID(self, value, field_name=None, config_file=None):
142148
#WRITE_TABLE_ID
143149
def WRITE_TABLE_ID(self, name, app_token=None, user_access_token=None, page_size=None, page_token=None, config_file=None):
144150
return WRITE_TABLE_ID(name, app_token, user_access_token, page_size, page_token, config_file)
145-
151+
152+
#WRITE_TENANT_ACCESS_TOKEN
153+
def WRITE_TENANT_ACCESS_TOKEN(self, app_id=None, app_secret=None, config_file=None):
154+
return WRITE_TENANT_ACCESS_TOKEN(app_id, app_secret, config_file)
155+
146156
#WRITE_VIEW_ID
147157
def WRITE_VIEW_ID(self, view_name, app_token=None, user_access_token=None, page_size=None, page_token=None, config_file=None):
148158
return WRITE_VIEW_ID(view_name, app_token, user_access_token, page_size, page_token, config_file)

GET_TENANT_ACCESS_TOKEN.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import requests
2+
import configparser
3+
import argparse
4+
import json
5+
6+
def GET_TENANT_ACCESS_TOKEN(app_id=None, app_secret=None, config_file=None):
7+
# 读取配置文件
8+
config = configparser.ConfigParser()
9+
10+
if config_file:
11+
config.read(config_file, encoding='utf-8')
12+
else:
13+
config.read('feishu-config.ini', encoding='utf-8')
14+
15+
# 从配置文件获取参数
16+
if app_id is None:
17+
app_id = config.get('ID', 'app_id')
18+
if app_secret is None:
19+
app_secret = config.get('ID', 'app_secret')
20+
21+
# 构建请求URL和请求头
22+
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
23+
headers = {
24+
'Content-Type': 'application/json; charset=utf-8',
25+
}
26+
27+
# 构建请求体
28+
payload = {
29+
"app_id": app_id,
30+
"app_secret": app_secret
31+
}
32+
33+
# 发起请求
34+
response = requests.post(url, headers=headers, data=json.dumps(payload))
35+
response_json = response.json()
36+
37+
# 更新配置文件
38+
# if 'app_access_token' in response_json:
39+
# config.set('TOKEN', 'app_access_token', response_json['app_access_token'])
40+
# if config_file:
41+
# with open(config_file, 'w', encoding='utf-8') as configfile:
42+
# config.write(configfile)
43+
# else:
44+
# with open('feishu-config.ini', 'w', encoding='utf-8') as configfile:
45+
# config.write(configfile)
46+
47+
return response_json.get('tenant_access_token')
48+
49+
def GET_TENANT_ACCESS_TOKEN_CMD():
50+
# 解析命令行参数
51+
parser = argparse.ArgumentParser()
52+
parser.add_argument('-i', '--id', help='app ID')
53+
parser.add_argument('-s', '--secret', help='app secret')
54+
parser.add_argument('--config_file', help='config file path')
55+
args = parser.parse_args()
56+
57+
# 调用GET_TENANT_ACCESS_TOKEN函数,获取tenant_access_token
58+
tenant_access_token = GET_TENANT_ACCESS_TOKEN(args.id, args.secret, args.config_file)
59+
60+
# 打印结果
61+
print(f'tenant_access_token: {tenant_access_token}')
62+
63+
# 主函数
64+
if __name__ == "__main__":
65+
GET_TENANT_ACCESS_TOKEN_CMD()

WRITE_TENANT_ACCESS_TOKEN.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import configparser
2+
from GET_TENANT_ACCESS_TOKEN import GET_TENANT_ACCESS_TOKEN
3+
import argparse
4+
5+
def WRITE_TENANT_ACCESS_TOKEN(app_id=None, app_secret=None, config_file=None):
6+
config = configparser.ConfigParser() # 创建一个ConfigParser对象
7+
if config_file:
8+
config.read(config_file, encoding='utf-8') # 读取指定的配置文件
9+
else:
10+
config.read('feishu-config.ini', encoding='utf-8') # 默认读取名为'feishu-config.ini'的配置文件
11+
12+
# 尝试从app_id和app_secret获取app_access_token
13+
try:
14+
app_access_token = GET_TENANT_ACCESS_TOKEN(app_id, app_secret, config_file)
15+
# 如果提取的值不存在,将其置为空字符串
16+
if not app_access_token:
17+
app_access_token = ''
18+
except Exception: # 如果在尝试过程中出现错误,返回None
19+
return None
20+
21+
# 检查配置文件是否存在名为'TOKEN'的section,如果不存在则添加
22+
if 'TOKEN' not in config:
23+
config.add_section('TOKEN')
24+
# 在'TOKEN' section下添加app_access_token
25+
config['TOKEN']['app_access_token'] = app_access_token
26+
27+
# 尝试将新的配置写入到配置文件中
28+
try:
29+
if config_file:
30+
with open(config_file, 'w', encoding='utf-8') as configfile:
31+
config.write(configfile)
32+
else:
33+
with open('feishu-config.ini', 'w', encoding='utf-8') as configfile:
34+
config.write(configfile)
35+
except Exception: # 如果在尝试过程中出现错误,返回None
36+
return None
37+
38+
return app_access_token # 如果一切正常,返回提取的值
39+
40+
# 主函数
41+
if __name__ == "__main__":
42+
# 解析命令行参数
43+
parser = argparse.ArgumentParser()
44+
parser.add_argument('-i', '--id', help='app ID')
45+
parser.add_argument('-s', '--secret', help='app secret')
46+
parser.add_argument('--config_file', help='config file path')
47+
args = parser.parse_args()
48+
49+
# 调用WRITE_TENANT_ACCESS_TOKEN函数,将从app_id和app_secret获取的app_access_token写入到配置文件中
50+
result = WRITE_TENANT_ACCESS_TOKEN(args.id, args.secret, args.config_file)
51+
52+
# 检查WRITE_TENANT_ACCESS_TOKEN函数的返回结果
53+
if result is None: # 如果返回None,打印错误信息
54+
print("发生错误,请检查您的输入并再试一次。")
55+
else: # 如果返回的不是None,打印提取的值和成功信息
56+
print(f"app_access_token: {result}")
57+
print("成功写入配置文件")

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='FeishuBitableAPI',
5-
version='3.2.9',
5+
version='3.3.0',
66
packages=find_packages(),
77
#py_modules=[],
88
url='https://github.com/BlueSkyXN/Feishu-Bitable-Python-API',

0 commit comments

Comments
 (0)