飞书|多维表格二次开发步骤

最近在接入飞书的多维表导入功能,摸索了好几天才搞明白

想要开发多维表格的话首先要手动创建一张多维表格

1.创建表格,放着先不动

2.工作台-创建应用

3.点右上角开发者管理后台

4.创建一个应用

5.创建应用后,这里面需要用到的信息App ID和App Secret

   这里的应用要发布后才能在表格里应用!!

6.发布应用等待审核通过后

   打开创建的多维表格右上角→···→更多→添加文档应用

7.测试代码

APP_ID、APP_SECRET在应用中

APP_TOKEN、TABLE_ID在多维表格URL中

/**
 * 飞书测试增删改查多维表格数据
 */
public class FeishuBitableDemo {

    // 飞书应用配置 (需替换为实际值)
    private static final String APP_ID = "cli_";
    private static final String APP_SECRET = "Exv1...";
    private static final String APP_TOKEN = "GK3e..."; // 多维表格的 app_token
    private static final String TABLE_ID = "tbl...";     // 多维表格的 table_id

    // API 地址
    private static final String GET_TOKEN_URL = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
    private static final String BITABLE_API = "https://open.feishu.cn/open-apis/bitable/v1/apps/%s/tables/%s/records";

    public static void main(String[] args) {
        try {
            String accessToken = getAccessToken();

            // 示例操作
            queryRecords(accessToken);       // 查询数据
            String recordId = addRecord(accessToken); // 新增数据
            System.out.println(recordId);
            updateRecord(accessToken, recordId);     // 更新数据
            deleteRecord(accessToken, recordId);     // 删除数据

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 1. 获取访问令牌
    private static String getAccessToken() throws IOException {
        OkHttpClient client = new OkHttpClient();
        JSONObject requestBody = new JSONObject()
                .put("app_id", APP_ID)
                .put("app_secret", APP_SECRET);

        Request request = new Request.Builder()
                .url(GET_TOKEN_URL)
                .post(RequestBody.create(requestBody.toString(), MediaType.parse("application/json")))
                .build();

        Response response = client.newCall(request).execute();
        JSONObject responseBody = new JSONObject(response.body().string());
        return responseBody.getString("tenant_access_token");
    }

    // 2. 查询多维表格记录
    private static void queryRecords(String accessToken) throws IOException {
        String url = String.format(BITABLE_API, APP_TOKEN, TABLE_ID);
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .get()
                .build();

        Response response = client.newCall(request).execute();
        JSONObject result = new JSONObject(response.body().string());
        System.out.println("查询结果: " + result.toString(2));
    }

    // 3. 新增记录
    private static String addRecord(String accessToken) throws IOException {
        String url = String.format(BITABLE_API, APP_TOKEN, TABLE_ID);
        OkHttpClient client = new OkHttpClient();

        // 构造字段数据(示例字段:标题、状态、负责人)
        Map<String, Object> fields = new HashMap<>();
        fields.put("文本", "王");
        fields.put("科目", "政治");
        fields.put("分数", "789"); // 用户ID数组

        JSONObject requestBody = new JSONObject()
                .put("fields", fields);

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .post(RequestBody.create(requestBody.toString(), MediaType.parse("application/json")))
                .build();

        Response response = client.newCall(request).execute();
        JSONObject result = null;
        if (response.body() != null) {
            result = new JSONObject(response.body().string());
        }
        if (result != null) {
            return result.getJSONObject("data").getJSONObject("record").getString("record_id");
        }
        return null;
    }

    // 4. 更新记录
    private static void updateRecord(String accessToken, String recordId) throws IOException {
        String url = String.format(BITABLE_API + "/%s", APP_TOKEN, TABLE_ID, recordId);
        OkHttpClient client = new OkHttpClient();

        Map<String, Object> fields = new HashMap<>();
        fields.put("文本", "已完成");

        JSONObject requestBody = new JSONObject()
                .put("fields", fields);

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .put(RequestBody.create(requestBody.toString(), MediaType.parse("application/json")))
                .build();

        Response response = client.newCall(request).execute();
        System.out.println("更新结果: " + response.body().string());
    }

    // 5. 删除记录
    private static void deleteRecord(String accessToken, String recordId) throws IOException {
        String url = String.format(BITABLE_API + "/%s", APP_TOKEN, TABLE_ID, recordId);
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", "Bearer " + accessToken)
                .delete()
                .build();

        Response response = client.newCall(request).execute();
        System.out.println("删除结果: " + response.body().string());
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值