npm install express
import express from 'express';
import { createFeishuRouter } from './fmode-server/modules/fmode-feishu-api/src/index.ts';
const app = express();
// 创建飞书路由(直接传入配置)
const feishuRouter = createFeishuRouter({
'cli_a1b2c3d4e5f6g7h8': {
appId: 'cli_a1b2c3d4e5f6g7h8',
appSecret: 'your_app_secret_here',
company: 'your_company_id',
enabled: true
}
});
// 挂载到Express应用
app.use('/api/feishu', feishuRouter);
app.listen(3000, () => {
console.log('服务器启动在端口 3000');
});
访问 http://localhost:3000/api/feishu/test 查看模块状态。
const feishuRouter = createFeishuRouter({
'cli_xxx': {
appId: 'cli_xxx', // 飞书应用ID
appSecret: 'your_secret', // 飞书应用密钥
company: 'company_id', // 公司ID(用于用户管理)
enabled: true // 是否启用
}
});
const feishuRouter = createFeishuRouter({
'app1': {
appId: 'cli_xxx1',
appSecret: 'secret1',
company: 'company1',
enabled: true
},
'app2': {
appId: 'cli_xxx2',
appSecret: 'secret2',
company: 'company2',
enabled: true
}
});
<!DOCTYPE html>
<html>
<head>
<title>飞书登录</title>
</head>
<body>
<button onclick="loginWithFeishu()">飞书扫码登录</button>
<script>
function loginWithFeishu() {
const appId = 'cli_a1b2c3d4e5f6g7h8';
const redirectUri = encodeURIComponent('http://localhost:3000/callback');
const state = Math.random().toString(36).substring(7);
const authUrl = `https://open.feishu.cn/open-apis/authen/v1/authorize?app_id=${appId}&redirect_uri=${redirectUri}&state=${state}`;
window.location.href = authUrl;
}
</script>
</body>
</html>
// POST /api/feishu/oauth2/login
fetch('/api/feishu/oauth2/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
appId: 'cli_a1b2c3d4e5f6g7h8',
code: '授权码'
})
})
.then(res => res.json())
.then(data => {
console.log('登录成功:', data.data.sessionToken);
});
<!DOCTYPE html>
<html>
<head>
<title>飞书应用</title>
<script src="https://lf1-cdn-tos.bytegoofy.com/goofy/lark/op/h5-js-sdk-1.5.16.js"></script>
</head>
<body>
<h1>飞书应用</h1>
<div id="user-info"></div>
<script>
const appId = 'cli_a1b2c3d4e5f6g7h8';
tt.ready(() => {
tt.requestAuthCode({
appId: appId,
success: (res) => {
const code = res.code;
loginWithCode(code);
},
fail: (err) => {
console.error('获取授权码失败:', err);
}
});
});
function loginWithCode(code) {
fetch('/api/feishu/oauth2/feishu', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
appId: appId,
code: code
})
})
.then(res => res.json())
.then(data => {
if (data.code === 1) {
const userInfo = data.data.userInfo;
document.getElementById('user-info').innerHTML = `
<p>欢迎,${userInfo.name}!</p>
<img src="${userInfo.avatarUrl}" width="50" />
`;
}
});
}
</script>
</body>
</html>
POST /api/feishu/oauth2/login{ appId, code }POST /api/feishu/oauth2/feishu{ appId, code }POST /api/feishu/oauth2/refresh_token{ appId, refreshToken }POST /api/feishu/user/sync{ appId, userInfo }POST /api/feishu/forward{ appId, path, method, query, body }POST /api/feishu/token/status{ appId }POST /api/feishu/token/refresh{ appId }import { createFeishuRouter, feishuConfig } from 'fmode-feishu-api';
// 需要先单独配置
feishuConfig.setAppConfig('cli_xxx', {
appId: 'cli_xxx',
appSecret: 'secret',
company: 'company_id',
enabled: true
});
// 然后创建路由
const feishuRouter = createFeishuRouter();
app.use('/api/feishu', feishuRouter);
import { createFeishuRouter } from 'fmode-feishu-api';
// 直接传入配置,一步完成
const feishuRouter = createFeishuRouter({
'cli_xxx': {
appId: 'cli_xxx',
appSecret: 'secret',
company: 'company_id',
enabled: true
}
});
app.use('/api/feishu', feishuRouter);
访问 飞书开放平台 创建企业自建应用。
在飞书开放平台的应用配置中,设置"重定向URL"。
tenant_access_token: 自动管理,有效期2小时,提前5分钟自动刷新user_access_token: 使用 /oauth2/refresh_token 接口刷新在 createFeishuRouter 中传入多个应用配置即可。