# File Upload + Send via Feishu API Verified working: 2026-06-29, Android/Termux. ## Upload File ```python import json, urllib.request TOKEN = 't-...' # from tenant_access_token FILE_PATH = '/path/to/file.tar.gz' FILE_NAME = 'file.tar.gz' with open(FILE_PATH, 'rb') as f: file_data = f.read() boundary = '----FormBoundary7MA4YWxkTrZu0gW' body = ( f'--{boundary}\r\n' f'Content-Disposition: form-data; name="file_name"\r\n\r\n{FILE_NAME}\r\n' f'--{boundary}\r\n' f'Content-Disposition: form-data; name="file_type"\r\n\r\nstream\r\n' f'--{boundary}\r\n' f'Content-Disposition: form-data; name="file"; filename="{FILE_NAME}"\r\n' f'Content-Type: application/octet-stream\r\n\r\n' ).encode() + file_data + f'\r\n--{boundary}--\r\n'.encode() req = urllib.request.Request( 'https://open.feishu.cn/open-apis/im/v1/files', data=body, headers={ 'Authorization': f'Bearer {TOKEN}', 'Content-Type': f'multipart/form-data; boundary={boundary}' }, method='POST' ) with urllib.request.urlopen(req, timeout=30) as r: result = json.loads(r.read()) file_key = result['data']['file_key'] ``` ## Send File Message ```python content = json.dumps({"file_key": file_key}) msg_data = json.dumps({ "receive_id": "ou_xxxxxxxx", # open_id "msg_type": "file", "content": content }).encode() req = urllib.request.Request( 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id', data=msg_data, headers={'Authorization': f'Bearer {TOKEN}', 'Content-Type': 'application/json'}, method='POST' ) with urllib.request.urlopen(req, timeout=15) as r: print(json.loads(r.read())) ``` ## Find User Open ID ```python req = urllib.request.Request( f'https://open.feishu.cn/open-apis/contact/v3/users?page_size=20&name={urllib.parse.quote("name")}', headers={'Authorization': f'Bearer {TOKEN}'} ) with urllib.request.urlopen(req, timeout=15) as r: data = json.loads(r.read()) for u in data.get('data', {}).get('items', []): print(f"{u['name']} — open_id: {u['open_id']}") ``` ## Token ```python req = urllib.request.Request( 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal', data=json.dumps({'app_id': APP_ID, 'app_secret': APP_SECRET}).encode(), headers={'Content-Type': 'application/json'}, method='POST' ) with urllib.request.urlopen(req, timeout=15) as r: token = json.loads(r.read())['tenant_access_token'] ```