FeishuNetSdk 1.0.6
See the version list below for details.
dotnet add package FeishuNetSdk --version 1.0.6
NuGet\Install-Package FeishuNetSdk -Version 1.0.6
<PackageReference Include="FeishuNetSdk" Version="1.0.6" />
<PackageVersion Include="FeishuNetSdk" Version="1.0.6" />
<PackageReference Include="FeishuNetSdk" />
paket add FeishuNetSdk --version 1.0.6
#r "nuget: FeishuNetSdk, 1.0.6"
#:package FeishuNetSdk@1.0.6
#addin nuget:?package=FeishuNetSdk&version=1.0.6
#tool nuget:?package=FeishuNetSdk&version=1.0.6
FeishuNetSdk
飞书开放平台网址:https://open.feishu.cn/
接口清单详见:TenantAccessToken适用接口清单-967个
用法:
1、安装Nuget包
2、依赖注册(直接输入应用凭证的方式,二选一)
builder.Services.AddHttpApi<IFeishuApi>();
builder.Services.AddHttpApi<IFeishuTenantApi>();
builder.Services.AddTokenProvider<IFeishuTenantApi>(async service =>
{
//获取凭证接口
var feishuApi = service.GetRequiredService<IFeishuApi>();
//获取tenant_access_token
var response = await feishuApi.PostAuthV3TenantAccessTokenInternalAsync(
new FeishuNetSdk.Auth.Spec
.PostAuthV3TenantAccessTokenInternalBodyDto
{
//此处修改为自建的应用凭证Id和密钥
AppId = "cli_a5bf8739dab8d0c",
AppSecret = "vn7MjifCNm04dUlWBg6yWbijHvEpel6G"
});
//返回一个能自动缓存和过期重取的Token实例
return new TokenResult
{
Access_token = response.TenantAccessToken,
Expires_in = response.Expire
};
});
2、依赖注册(配置文件的方式,二选一)
- 创建
FeishuOption类:
public record FeishuOption
{
public string app_id { get; set; } = string.Empty;
public string app_secret { get; set; } = string.Empty;
}
- 在
appsettings.json文件中添加配置:
"Feishu": {
"app_id": "cli_a5bf8739dab8d0c",
"app_secret": "vn7MjifCNm04dUlWBg6yWbijHvEpel6G",
}
- 添加绑定配置:
builder.Services.Configure<FeishuOption>(builder.Configuration.GetSection("Feishu"));
- 接口注册:
builder.Services.AddHttpApi<IFeishuApi>();
builder.Services.AddHttpApi<IFeishuTenantApi>();
builder.Services.AddTokenProvider<IFeishuTenantApi>(async service =>
{
//获取配置
var option = service.GetRequiredService<IOptionsMonitor<FeishuOption>>();
//获取凭证接口
var feishuApi = service.GetRequiredService<IFeishuApi>();
//获取tenant_access_token
var response = await feishuApi.PostAuthV3TenantAccessTokenInternalAsync(
new FeishuNetSdk.Auth.Spec
.PostAuthV3TenantAccessTokenInternalBodyDto
{
AppId = option.CurrentValue.app_id,
AppSecret = option.CurrentValue.app_secret
});
//返回一个能自动缓存和过期重取的Token实例
return new TokenResult
{
Access_token = response.TenantAccessToken,
Expires_in = response.Expire
};
});
3、依赖注入
public class TestController : ControllerBase
{
private readonly IFeishuTenantApi _feishuApi;
public TestController(IFeishuTenantApi feishuApi)
{
_feishuApi = feishuApi;
}
}
4、方法调用
[HttpGet("t2")]
public async Task<IResult> GetT2Async()
{
var result2 = await _feishuApi.GetImV1ChatsAsync();
return Results.Json(result2);
}
示例:
文件上传示例
参数 FormDataFile 支持 filePath、FileInfo、byte[]、Stream。
需要注意部分接口注释上有关于文件格式限制的说明。
[HttpGet("t3")]
public async Task<IResult> GetT3Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f1d7a4c4f992724f5de44bb14.jpg";
//调用接口
var result = await _feishuApi.PostImV1ImagesAsync(
new FeishuNetSdk.Im.PostImV1ImagesBodyDto
{
ImageType = "message"
},
new FormDataFile(filePath));
//当返回码不成功时,返回错误消息
if (!result.IsSuccess)
return Results.Problem(result.Msg);
return Results.Json(result);
}
文件下载示例
下载操作默认返回HttpResponseMessage,由于没有返回码(code)可以判断操作是否成功,所以建议配合 EnsureSuccessStatusCode() 方法使用,当响应状态码异常时,会抛出异常,需要自行捕获处理。
[HttpGet("t4")]
public async Task<IResult> GetT4Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f----1.jpg";
//调用接口
var result = (await _feishuApi.GetImV1ImagesByImageKeyAsync(
"img_xxxx-fbdc-4c36-b17c-ac8aa1aee7dg"))
//当响应状态码异常时,会抛出异常,需要自行捕获处理
.EnsureSuccessStatusCode();
//保存文件到指定路径
await result.SaveAsAsync(filePath);
return Results.Json(result);
}
个别接口支持部分下载,可以按需设置参数Range,字符串格式为bytes=0-100表示下载第0字节到第100字节的数据,默认不填或者null表示下载整个文件。示例如下:
[HttpGet("t5")]
public async Task<IResult> GetT5Async()
{
//定义文件存储路径
var filePath = @"D:\Users\Downloads\e9bd937f----2.jpg";
//调用接口
var result = (await _feishuApi.GetDriveV1MediasByFileTokenDownloadAsync(
"OQBpbF8AEoZ0gqxpCMwcRPWFn8c",
"bytes=0-100"))
//当响应状态码异常时,会抛出异常,需要自行捕获处理
.EnsureSuccessStatusCode();
//保存到指定路径,可能只是文件的一部分,并非完整。
await result.SaveAsAsync(filePath);
return Results.Json(result);
}
注意事项:
云文档操作
文档操作前提需要有编辑权限,步骤如下:
- 为
自建应用添加机器人能力。 - 将
应用机器人加入或创建一个新群组。 - 进入目标文档,将该
群组设置为文档协作者。 - 调用接口方法。
以下是仅在特殊情况下使用的特殊方法。
接口重载/覆盖
- 新建API,继承于
IFeishuTenantApi。 - 使用重载/覆盖方法
如果要覆盖方法,比如是在保持参数完全一致的情况下,修改http地址,需要在方法前加 new (参数不一致是重载,重载不用加new ),然后将新地址更换到属性上。更换http方法、返回参数及其他属性也是同理。
1、新建API
public interface INewApi : IFeishuTenantApi
{
[HttpGet("/open-apis/event/v1/outbound_ip1")]
new System.Threading.Tasks.Task<HttpResponseMessage> GetEventV1OutboundIpAsync();
}
2、新增依赖注册
builder.Services.AddHttpApi<INewApi>();
3、修改依赖注入
public class TestController : ControllerBase
{
//此处更改为新的API:INewApi
private readonly INewApi _feishuApi;
public TestController(INewApi feishuApi)
{
_feishuApi = feishuApi;
}
}
启用状态异常错误
默认:关闭
飞书接口在返回结果异常时,同时会返回状态异常,状态异常通常无法实质判断异常原因,具体原因会在返回结果中提示。所以接口默认忽略状态异常,开启方法如下:
- 新建API,继承于
IFeishuTenantApi。 - 添加接口属性
IgnoreStatusExceptionFilter并将属性Enable设置为false,意为停用忽略,即启用异常提示。 - 参照上述使用新接口进行方法调用。
[IgnoreStatusExceptionFilter(Enable = false)]
public interface INewApi : IFeishuTenantApi
{ }
开启状态异常之后的捕获方法:
try
{
var result2 = await _feishuApi.GetEventV1OutboundIpAsync();
return Results.Json(result2);
}
catch (HttpRequestException ex) when (ex.InnerException is ApiResponseStatusException statusException)
{
// 响应状态码异常
return Results.Problem(statusException.Message);
}
关闭接口日志
默认:开启
- 新建API,继承于
IFeishuTenantApi。 - 添加接口属性
LoggingFilter并设置属性Enable为false,即可停用日志。 - 参照上述使用新接口进行方法调用。
LoggingFilter和IgnoreStatusExceptionFilter可以同时存在。
[LoggingFilter(Enable = false), IgnoreStatusExceptionFilter(Enable = false)]
public interface INewApi : IFeishuTenantApi
{ }
需要注意: IFeishuApi 和 IFeishuTenantApi 各有独立的 LoggingFilter 与 IgnoreStatusExceptionFilter 属性,若想全部关闭,需要分别继承接口并将属性 Enable 设置为 false 。
启用缓存
默认:关闭
缓存属性Cache,在接口上使用表示对接口内所有方法启用,建议仅针对具体方法使用,在单个方法上增加属性即可。数值单位是毫秒。
public interface INewApi : IFeishuTenantApi
{
[Cache(10 * 1000)]
[HttpGet("/open-apis/event/v1/outbound_ip1")]
new System.Threading.Tasks.Task<HttpResponseMessage> GetEventV1OutboundIpAsync();
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
- WebApiClientCore.Extensions.NewtonsoftJson (>= 2.0.4)
- WebApiClientCore.Extensions.OAuths (>= 2.0.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on FeishuNetSdk:
| Package | Downloads |
|---|---|
|
FeishuNetSdk.WebSocket
适用于飞书开放平台的.Net开发包 |
|
|
FeishuNetSdk.Endpoint
适用于飞书开放平台的.Net开发包 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.6.0 | 47 | 11/8/2025 |
| 3.5.9 | 142 | 10/31/2025 |
| 3.5.8 | 119 | 10/24/2025 |
| 3.5.7 | 126 | 10/18/2025 |
| 3.5.6 | 184 | 10/1/2025 |
| 3.5.5 | 261 | 9/19/2025 |
| 3.5.4 | 182 | 9/12/2025 |
| 3.5.3 | 204 | 9/5/2025 |
| 3.5.2 | 209 | 8/29/2025 |
| 3.5.1 | 147 | 8/23/2025 |
| 3.5.0 | 267 | 8/16/2025 |
| 3.4.9 | 151 | 8/9/2025 |
| 3.4.8 | 144 | 8/1/2025 |
| 3.4.7 | 284 | 7/26/2025 |
| 3.4.6 | 125 | 7/18/2025 |
| 3.4.5 | 131 | 7/11/2025 |
| 3.4.4 | 360 | 7/5/2025 |
| 3.4.3 | 130 | 6/27/2025 |
| 3.4.2 | 138 | 6/21/2025 |
| 3.4.1 | 261 | 6/13/2025 |
| 3.4.0 | 179 | 6/6/2025 |
| 3.3.9 | 134 | 5/30/2025 |
| 3.3.8 | 130 | 5/25/2025 |
| 3.3.7 | 209 | 5/18/2025 |
| 3.3.6 | 212 | 5/6/2025 |
| 3.3.5 | 215 | 4/18/2025 |
| 3.3.4 | 160 | 4/12/2025 |
| 3.3.3 | 151 | 4/5/2025 |
| 3.3.2 | 204 | 3/30/2025 |
| 3.3.1 | 170 | 3/21/2025 |
| 3.3.0 | 181 | 3/14/2025 |
| 3.2.9 | 294 | 3/7/2025 |
| 3.2.8 | 217 | 2/28/2025 |
| 3.2.7 | 220 | 2/21/2025 |
| 3.2.6 | 285 | 2/14/2025 |
| 3.2.5 | 295 | 2/9/2025 |
| 3.2.4 | 300 | 2/5/2025 |
| 3.2.3 | 358 | 1/17/2025 |
| 3.2.2 | 246 | 1/11/2025 |
| 3.2.1 | 454 | 1/6/2025 |
| 3.2.0 | 178 | 12/27/2024 |
| 3.1.9 | 186 | 12/21/2024 |
| 3.1.8 | 261 | 12/15/2024 |
| 3.1.7 | 505 | 12/8/2024 |
| 3.1.6 | 322 | 11/30/2024 |
| 3.1.5 | 294 | 11/27/2024 |
| 3.1.4 | 177 | 11/22/2024 |
| 3.1.3 | 192 | 11/15/2024 |
| 3.1.2 | 167 | 11/8/2024 |
| 3.1.1 | 276 | 11/3/2024 |
| 3.1.0 | 399 | 10/26/2024 |
| 3.0.9 | 310 | 10/23/2024 |
| 3.0.8 | 365 | 10/19/2024 |
| 3.0.7 | 194 | 10/12/2024 |
| 3.0.6 | 199 | 10/9/2024 |
| 3.0.5 | 162 | 9/27/2024 |
| 3.0.4 | 208 | 9/24/2024 |
| 3.0.3 | 185 | 9/21/2024 |
| 3.0.2 | 207 | 9/18/2024 |
| 3.0.1 | 209 | 9/13/2024 |
| 3.0.0 | 769 | 9/8/2024 |
| 2.4.6 | 276 | 9/1/2024 |
| 2.4.5 | 165 | 8/28/2024 |
| 2.4.4 | 222 | 8/24/2024 |
| 2.4.3 | 198 | 8/15/2024 |
| 2.4.2 | 191 | 8/8/2024 |
| 2.4.1 | 138 | 8/2/2024 |
| 2.4.0 | 433 | 7/30/2024 |
| 2.3.9 | 195 | 7/24/2024 |
| 2.3.8 | 182 | 7/22/2024 |
| 2.3.7 | 183 | 7/18/2024 |
| 2.3.6 | 187 | 7/12/2024 |
| 2.3.5 | 175 | 7/9/2024 |
| 2.3.4 | 181 | 7/4/2024 |
| 2.3.3 | 170 | 7/1/2024 |
| 2.3.2 | 186 | 6/27/2024 |
| 2.3.1 | 213 | 6/23/2024 |
| 2.3.0 | 185 | 6/20/2024 |
| 2.2.9 | 204 | 6/14/2024 |
| 2.2.8 | 182 | 6/7/2024 |
| 2.2.7 | 576 | 6/3/2024 |
| 2.2.6 | 162 | 5/28/2024 |
| 2.2.5 | 209 | 5/23/2024 |
| 2.2.4 | 163 | 5/13/2024 |
| 2.2.3 | 163 | 5/10/2024 |
| 2.2.2 | 196 | 4/30/2024 |
| 2.2.1 | 192 | 4/27/2024 |
| 2.2.0 | 204 | 4/23/2024 |
| 2.1.9.1 | 182 | 4/19/2024 |
| 2.1.8 | 246 | 4/11/2024 |
| 2.1.7 | 264 | 4/9/2024 |
| 2.1.6 | 184 | 4/2/2024 |
| 2.1.5 | 204 | 3/29/2024 |
| 2.1.4 | 178 | 3/26/2024 |
| 2.1.3 | 241 | 3/19/2024 |
| 2.1.2 | 204 | 3/7/2024 |
| 2.1.1 | 214 | 3/1/2024 |
| 2.1.0 | 199 | 2/20/2024 |
| 2.0.9 | 204 | 2/5/2024 |
| 2.0.8 | 425 | 1/26/2024 |
| 2.0.7 | 184 | 1/23/2024 |
| 2.0.6 | 213 | 1/12/2024 |
| 2.0.5 | 193 | 1/9/2024 |
| 2.0.4 | 199 | 1/4/2024 |
| 2.0.3 | 214 | 12/28/2023 |
| 2.0.2 | 201 | 12/21/2023 |
| 2.0.1 | 1,040 | 12/12/2023 |
| 2.0.0 | 224 | 12/6/2023 |
| 1.0.13 | 194 | 11/13/2023 |
| 1.0.12 | 196 | 11/12/2023 |
| 1.0.11 | 186 | 11/9/2023 |
| 1.0.10 | 201 | 11/7/2023 |
| 1.0.9 | 283 | 11/2/2023 |
| 1.0.8 | 174 | 10/31/2023 |
| 1.0.7 | 193 | 10/27/2023 |
| 1.0.6 | 200 | 10/26/2023 |
| 1.0.5 | 214 | 10/26/2023 |