最近在使用飞书,想通过接口来下载飞书文档
https://open.feishu.cn/api-explorer/cli_a5049e070838d00c?apiName=download
发现无法将二进制流转换为文件
后来发现其文档有一些谬误,文档上写的是response.text 实际写入二进制文件需要的是response.content
#发起下载请求,拿到文件的ticket_id xlsx_token='' #excel的token,其实就是飞书文档网址的最后一长串字符串 #tenant_access_token_actual='' #tenant_access_token 这个用飞书接口都知道是啥,类似于用户秘钥 def send_download_request(xlsx_token,tenant_access_token_actual): import requests import json url = "https://open.feishu.cn/open-apis/drive/v1/export_tasks" payload = json.dumps({"file_extension": "xlsx", "sub_id": "", "token": xlsx_token, "type": "sheet" }) headers = {'Content-Type': 'application/json', 'Authorization': tenant_access_token_actual } response = requests.request("POST", url, headers=headers, data=payload) send_download_request_json=json.loads(response.text)['data']['ticket'] return send_download_request_json ticket_id=send_download_request(xlsx_token,tenant_access_token_actual) print(ticket_id) #获取下载信息,得知文件准备下载成功后,获取file_token def get_download_status(ticket_id,xlsx_token,tenant_access_token_actual): import requests url = "https://open.feishu.cn/open-apis/drive/v1/export_tasks/"+ticket_id+"?token="+xlsx_token payload = '' headers = { 'Authorization': tenant_access_token_actual } response = requests.request("GET", url, headers=headers, data=payload) print(response) file_token=json.loads(response.text)['data']['result']['file_token'] return file_token #xlsx_token='' file_token=get_download_status(ticket_id,xlsx_token,tenant_access_token_actual) print(file_token) #进行下载 def download_file(file_token,tenant_access_token_actual,file_loc): import requests import shutil url = "https://open.feishu.cn/open-apis/drive/v1/export_tasks/file/"+file_token+"/download" payload = '' print(url) headers = { 'Authorization': tenant_access_token_actual } response = requests.request("GET", url, headers=headers, data=payload) print(response) # 注意,要用wb来以二进制形式写入数据,其次,要用response.content来获取二进制内容 #with open(file_loc, 'wb',encoding='utf8') as f: with open(file_loc, 'wb') as f: f.write(response.content) return file_loc file_token='' file_loc=r'E:\EXCEL抓数\file\1.xlsx' file_loc_path=download_file(file_token,tenant_access_token_actual,file_loc) print(file_loc_path)
标签:Python,access,二进制,token,tenant,file,download,response,下载 From: https://www.cnblogs.com/castlevania/p/18008421