1 | |
2 |
|
3 | type Linkedin = { |
4 | token: string; |
5 | }; |
6 | export async function main( |
7 | auth: Linkedin, |
8 | content: string, |
9 | visibility: "PUBLIC" | "CONNECTIONS" = "PUBLIC", |
10 | ) { |
11 | const entityResponse = await fetch("https://api.linkedin.com/v2/me", { |
12 | headers: { Authorization: "Bearer " + auth.token }, |
13 | }); |
14 | const entityId = (await entityResponse.json()).id; |
15 |
|
16 | const url = new URL("https://api.linkedin.com/v2/ugcPosts"); |
17 | const body = JSON.stringify({ |
18 | author: `urn:li:person:${entityId}`, |
19 | lifecycleState: "PUBLISHED", |
20 | specificContent: { |
21 | "com.linkedin.ugc.ShareContent": { |
22 | shareCommentary: { |
23 | text: content, |
24 | }, |
25 | shareMediaCategory: "NONE", |
26 | }, |
27 | }, |
28 | visibility: { |
29 | "com.linkedin.ugc.MemberNetworkVisibility": visibility, |
30 | }, |
31 | }); |
32 | const response = await fetch(url, { |
33 | method: "POST", |
34 | headers: { |
35 | Authorization: "Bearer " + auth.token, |
36 | "X-Restli-Protocol-Version": "2.0.0", |
37 | }, |
38 | body, |
39 | }); |
40 |
|
41 | if (!response.ok) { |
42 | throw Error(await response.text()); |
43 | } |
44 | return await response.json(); |
45 | } |
46 |
|