LinkedIn REST API允许开发者通过编程方式与LinkedIn平台交互,包括在群组中发布内容。这是LinkedIn提供给开发者的官方接口,遵循OAuth 2.0认证标准。
首先需要创建LinkedIn开发者应用并获取必要的API密钥:
使用OAuth 2.0获取访问令牌:
// 示例:获取授权码
const authUrl = `https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&state=${STATE}&scope=${SCOPES}`;
// 示例:交换访问令牌
async function getAccessToken(code) {
const response = await fetch('https://www.linkedin.com/oauth/v2/accessToken', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'authorization_code',
code: code,
redirect_uri: REDIRECT_URI,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
}),
});
return await response.json();
}
使用获取的访问令牌发布内容到群组:
async function postToGroup(groupId, content, accessToken) {
const postUrl = `https://api.linkedin.com/v2/ugcPosts`;
const postData = {
author: `urn:li:person:${YOUR_MEMBER_ID}`,
lifecycleState: "PUBLISHED",
specificContent: {
"com.linkedin.ugc.ShareContent": {
shareCommentary: {
text: content
},
shareMediaCategory: "NONE"
}
},
visibility: {
"com.linkedin.ugc.MemberNetworkVisibility": "CONTAINER",
container: `urn:li:group:${groupId}`
}
};
const response = await fetch(postUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Restli-Protocol-Version': '2.0.0',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(postData)
});
return await response.json();
}
w_member_social
通过以上步骤和注意事项,你可以成功使用LinkedIn REST API在群组中发布内容。