post image 错误
https://open.feishu.cn/api-explorer/cli_a38f3df0e8f8100b?apiName=create&project=im&resource=image&version=v1
使用其提供的示例代码,
// node-sdk使用说明:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/nodejs-sdk/preparation-before-development
// 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用
const lark = require('@larksuiteoapi/node-sdk');
const fs = require('fs');
// 开发者复制该Demo后,需要修改Demo里面的"app id", "app secret"为自己应用的appId, appSecret
const client = new lark.Client({
appId: 'app id',
appSecret: 'app secret',
// disableTokenCache为true时,SDK不会主动拉取并缓存token,这时需要在发起请求时,调用lark.withTenantToken("token")手动传递
// disableTokenCache为false时,SDK会自动管理租户token的获取与刷新,无需使用lark.withTenantToken("token")手动传递token
disableTokenCache: true
});
const file = fs.readFileSync('output.png');
client.im.v1.image.create({
data: {
image_type: 'avatar',
image: file,
},
},
lark.withTenantToken("t-g1041l9DGV7LG4FRQU7HEVYPLGZXJ4VGEK6CE7IU")
).then(res => {
console.log(res);
}).catch(e => {
console.error(JSON.stringify(e.response.data, null, 4));
});
使用node sdk上传,
TypeError: source.on is not a function
at DelayedStream.create (/www/my-app/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at CombinedStream.append (/www/my-app/node_modules/combined-stream/lib/combined_stream.js:46:37)
at FormData.append (/www/my-app/node_modules/form-data/lib/form_data.js:75:3)
at build (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/helpers/toFormData.js:63:16)
at each (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/helpers/toFormData.js:58:9)
at Object.forEach (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/utils.js:276:12)
at build (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/helpers/toFormData.js:40:13)
at toFormData (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/helpers/toFormData.js:67:3)
at Object.transformRequest (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/defaults/index.js:80:14)
at transform (/www/my-app/node_modules/@larksuiteoapi/node-sdk/node_modules/axios/lib/core/transformData.js:18:15)
这里的核心原因是,client.im.v1.image.create的方法参数签名允许data是Buffer | ReadableStream 但是Buffer传进去抛错 实际上,我这里上传图片是动态生成的buffer而不是从文件读取 当然我可以在调用前把变量从buffer转换成ReadableStream,但是这一步操作应该在sdk进行,或者去掉允许Buffer作为参数
现在的实际效果是,根据sdk以及 飞书官网文档上的示例,报错
确实是个问题;因为无法知晓sdk运行环境的node版本,所以类型是Buffer | ReadableStream,sdk内部转的话也会有break change
可以知道啊,process.version能拿到nodejs版本 更简单的,Blob是个全局类,如果存在这个类就直接把Buffer转换成ReadableStream 现在的新项目用node16或更早的版本已经很少了,总不能官网示例一直出错吧
可以知道啊,process.version能拿到nodejs版本 更简单的,Blob是个全局类,如果存在这个类就直接把Buffer转换成ReadableStream 现在的新项目用node16或更早的版本已经很少了,总不能官网示例一直出错吧
OK~感谢同学提供思路,我看看怎么样提供好点儿
这个问题两年了都还没有解决吗,文档也不更新说明下。不仅仅上传图片会出现这个问题,上传文件也会出现