I want to share a post using the LinkedIn Post API.
async function shareOnLinkedin(authCode:string,accessToken: string) {
const ME_RESOURCE = 'https://api.linkedin.com/v2/userinfo';
const POSTS_RESOURCE = 'https://api.linkedin.com/v2/ugcPosts';
const API_VERSION = '202302';
try {
// Fetch user profile to get user ID
const meResponse = await fetch(ME_RESOURCE, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
}
});
if (!meResponse.ok) {
throw new Error(`Error fetching profile: ${meResponse.statusText}`);
}
const meData = await meResponse.json();
console.log('User profile:', meData);
// Create a new post
const postResponse = await axios.post(POSTS_RESOURCE, {
//method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'X-RestLi-Protocol-Version': '2.0.0'
},
body: JSON.stringify({
"author": `urn:li:person:${meData.id}`,
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "Learning more about LinkedIn by reading the LinkedIn Blog!"
},
"shareMediaCategory": "ARTICLE",
"media": [
{
"status": "READY",
"description": {
"text": "Official LinkedIn Blog - Your source for insights and information about LinkedIn."
},
"originalUrl": "https://blog.linkedin.com/",
"title": {
"text": "Official LinkedIn Blog"
}
}
]
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
})
});
// if (!postResponse) {
// throw new Error(`Error creating post: ${postResponse.statusText}`);
// }
const postData = await postResponse.data();
console.log('Post created:', postData);
// This is the created share URN
console.log('Created post ID:', postData.id);
} catch (error) {
console.error('Error sharing post:', error);
}
}
I am exectuting this function from a supabase edge function.
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'X-RestLi-Protocol-Version': '2.0.0'
},
The token used in the header is created from the LinkedIn Developer dashboard OAuth tool.
-
I attempted using the 3 legged Auth flow to generate the access token but the response is still 401 unauthorized.
-
I attempted using the 3 legged Auth flow to generate auth code but the response is still 401 unauthorized.
-
I attemped using the LinkedIn API SDK but getting 426 Request failed
async function shareOnLinkedinOld(authCode:string,accessToken: string){
const ME_RESOURCE = '/userinfo';
const UGC_POSTS_RESOURCE = '/ugcPosts';
const POSTS_RESOURCE = '/posts';
const API_VERSION = '202302';
const restliClient = new RestliClient();
restliClient.setDebugParams({ enabled: true });
const meResponse = await restliClient.get({
resourcePath: ME_RESOURCE,
accessToken
});
console.log(meResponse.data);
const postsCreateResponse = await restliClient.create({
resourcePath: UGC_POSTS_RESOURCE,
entity: {
author: `urn:li:person:${meResponse.data.id}`,
lifecycleState: 'PUBLISHED',
visibility: 'PUBLIC',
commentary: 'Sample text post created with /posts API',
distribution: {
feedDistribution: 'MAIN_FEED',
targetEntities: [],
thirdPartyDistributionChannels: []
}
},
accessToken:accessToken,
versionString: API_VERSION
}).catch((error) => {
console.error('Error Sharing Post:', error);
throw error; // Re-throw the error if you want to handle it later
});
// This is the created share URN
console.log(postsCreateResponse.createdEntityId);
}
I have validated the access Token on the OAuth Tools on the developer dashboard. The access Token is active and not expired.
What could i be doing wrong?