-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcreate-posts.ts
More file actions
92 lines (84 loc) · 2.71 KB
/
create-posts.ts
File metadata and controls
92 lines (84 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Example calls to create a post on LinkedIn. This requires a member-based token with the following
* scopes (r_liteprofile, w_member_social), which is provided by the Sign in with LinkedIn and Share on LinkedIn
* API products.
*
* The steps include:
* 1. Fetching the authenticated member's profile to obtain the member's identifier (a person URN)
* 2. Create a post using /ugcPosts endpoint (legacy) or /posts endpoint (new)
*
* To view these posts, go to linkedin.com and click Me > Posts & Activity.
*
* BEWARE: This will make an actual post to the main feed which is visible to anyone.
*/
import { RestliClient } from 'linkedin-api-client';
import dotenv from 'dotenv';
dotenv.config();
const ME_RESOURCE = '/me';
const UGC_POSTS_RESOURCE = '/ugcPosts';
const POSTS_RESOURCE = '/posts';
const API_VERSION = '202302';
async function main(): Promise<void> {
const restliClient = new RestliClient();
restliClient.setDebugParams({ enabled: true });
const accessToken = process.env.ACCESS_TOKEN || '';
const meResponse = await restliClient.get({
resourcePath: ME_RESOURCE,
accessToken
});
console.log(meResponse.data);
/**
* Calling the legacy /ugcPosts API to create a text post on behalf of the
* authenticated member.
*/
const ugcPostsCreateResponse = await restliClient.create({
resourcePath: UGC_POSTS_RESOURCE,
entity: {
author: `urn:li:person:${meResponse.data.id}`,
lifecycleState: 'PUBLISHED',
specificContent: {
'com.linkedin.ugc.ShareContent': {
shareCommentary: {
text: 'Sample text post created with /ugcPosts API'
},
shareMediaCategory: 'NONE'
}
},
visibility: {
'com.linkedin.ugc.MemberNetworkVisibility': 'PUBLIC'
}
},
accessToken
});
// This is the created share URN
console.log(ugcPostsCreateResponse.createdEntityId);
/**
* Calling the newer, more streamlined (and versioned) /posts API to create
* a text post on behalf of the authenticated member.
*/
const postsCreateResponse = await restliClient.create({
resourcePath: 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,
versionString: API_VERSION
});
// This is the created share URN
console.log(postsCreateResponse.createdEntityId);
}
main()
.then(() => {
console.log('Completed');
})
.catch((error) => {
console.log(`Error encountered: ${error.message}`);
});