1
Create a Simple Post (User)
One script reply has been approved by the moderators Verified
Created by aureliemysticfairy88 902 days ago Viewed 9308 times
0
Submitted by adam186 Deno
Verified 715 days ago
1
type Linkedin = {
2
  token: string;
3
};
4
export async function main(
5
  auth: Linkedin,
6
  content: string,
7
  visibility: "PUBLIC" | "CONNECTIONS" = "PUBLIC",
8
) {
9
  const entityResponse = await fetch("https://api.linkedin.com/v2/me", {
10
    headers: { Authorization: "Bearer " + auth.token },
11
  });
12
  const entityId = (await entityResponse.json()).id;
13

14
  const url = new URL("https://api.linkedin.com/v2/ugcPosts");
15
  const body = JSON.stringify({
16
    author: `urn:li:person:${entityId}`,
17
    lifecycleState: "PUBLISHED",
18
    specificContent: {
19
      "com.linkedin.ugc.ShareContent": {
20
        shareCommentary: {
21
          text: content,
22
        },
23
        shareMediaCategory: "NONE",
24
      },
25
    },
26
    visibility: {
27
      "com.linkedin.ugc.MemberNetworkVisibility": visibility,
28
    },
29
  });
30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
      "X-Restli-Protocol-Version": "2.0.0",
35
    },
36
    body,
37
  });
38

39
  if (!response.ok) {
40
    throw Error(await response.text());
41
  }
42
  return await response.json();
43
}
44

Other submissions