Python调用飞书Api上传本表格

Python调用飞书Api上传本表格

import requests
# https://open.feishu.cn/open-apis/authen/v1/index?redirect_uri=?&app_id=?_a1039072c9f8900c
class FeishuApi():
  def __init__(self,app_id,app_secret,code_x):
    self.app_id = app_id
    self.app_secret = app_secret
    self.code_x = code_x
  def app_access_token(self): # 获取企业自建应用app_token
    data = {
      "app_id":self.app_id,
      "app_secret":self.app_secret
    }
    app_token = requests.post("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", json=data)
    return app_token.json()

  def user_access_token(self):
    data = {
     "grant_type": "authorization_code",
     "code": self.code_x
    }
    headers = {
            "Authorization":"Bearer "+self.app_access_token()["tenant_access_token"]
        }
    user_token = requests.post("https://open.feishu.cn/open-apis/authen/v1/access_token", json=data ,headers=headers)
    return user_token.json()["data"]["access_token"]

if __name__ == '__main__':
    Fa = FeishuApi(app_id="",
                   app_secret="",
                   code_x = "")
    user_token = Fa.user_access_token()
    print(user_token)

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
【案例】 【案例】Python飞书机器⼈编辑表格 飞书机器⼈编辑表格 #!/usr/bin/env python # -*- coding: utf-8 -*- from .config import * import requests import json class Bot: def __init__(self, app): self.app = app # app为机器⼈ self.table = None # 共享⽂档的table_id self.sheet = None # 共享⽂档的sheet_id self.token = self.get_token() self.header = { "Authorization": self.token, "Content-Type": "application/json; charset=utf-8" } def get_token(self): """获取应⽤token""" url = url_api['url_token'] headers = {"Content-Type": "text/plain"} r = requests.post(url, headers=headers, json=self.app) return "Bearer " + json.loads(r.text)['tenant_access_token'] def use(self, table, sheet): self.table = table self.sheet = sheet return self def add_data(self, sheet_range="", values=[]): """增加数据,原始数据下移""" data = { "valueRange": { "range": self.sheet + sheet_range, "values": values } } r = requests.post(urls['插⼊数据'] % self.table, headers=self.header, json=data) return r.text def del_data(self, major=0, start_index=1, end_index=1): """删除数据""" data = { "dimension": { "sheetId": self.sheet, "majorDimension": ['ROWS', 'COLUMNS'][major], "startIndex": start_index, "endIndex": end_index } } r = requests.delete(urls['删除数据'] % self.table, headers=self.header, json=data) return r.text def union_cell(self, sheet_range, major=0): """合并单元格""" url = urls['合并单元格'] % self.table data = { "range": self.sheet + sheet_range, "mergeType": ['MERGE_ALL', 'MERGE_ROWS', 'MERGE_COLUMNS'][major] } r = requests.post(url, headers=self.header, json=data) return r.text def split_cell(self, sheet_range): """拆分单元格""" data = { "range": self.sheet + sheet_range } r = requests.post(urls['拆分单元格'] % self.table, headers=self.header, json=data) return r.text def set_style(self, sheet_range, kind): def set_style(self, sheet_range, kind): """设置单元格格式""" data = { "appendStyle": { "range": self.sheet + sheet_range, "style": self.style_list(kind) } } r = requests.put(urls['设置单元格格式'] % self.table, headers=self.header, json=data) return r.text def phone_to_open_id(self, mobile

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值