【发布时间】:2020-12-09 04:22:53
【问题描述】:
我正在尝试使用 LinkedIn API to create a post 使用 Node & Express 和 node-fetch package 来发出请求,但是我似乎总是收到错误消息“创建方法请求不允许使用空实体主体”并且我不知道如何解决错误。我已将正文包含在请求中,但我想不是以正确的方式。我也收到错误代码 400,所以我知道我做错了什么,我只是不确定是什么。
我的问题是:
- 什么是实体主体?
- 如何将其添加到我的请求中?
function newPost(req, done) {
const id = "XXX";
const url = "https://api.linkedin.com/v2/ugcPosts";
const title = "Title title";
const text = "Text text";
const body = {
owner: `urn:li:person:${id}`,
subject: title,
text: {
text: text,
},
content: {
title: title,
},
};
const headers = {
'Authorization': `Bearer ${YYY}`,
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0',
'x-li-format': 'json',
}
fetch(url, {
method: "POST",
headers: headers,
json: body,
}).then(res => res.json()).then(result => {
console.log(result);
const message = {
text: "Posted",
success: true,
}
done(message, "linkedin");
}).catch(err => {
console.log(err);
const message = {
text: "Failed",
success: false,
}
done(message, "linkedin");
});
}
【问题讨论】:
-
根据给定的API URL链接应该是
https://api.linkedin.com/v2/ugcPosts而不是https://api.linkedin.com/v2/shares -
删除linkedin标签,因为这似乎是您如何使用获取的问题,而不是您使用LinkedIn API的问题。