Create a Simple Post (User)

Script linkedin Verified

by aureliemysticfairy88 ยท 6/6/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 days ago
1
//native
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

Other submissions
  • Submitted by aahaanjokes920 Deno
    Created 1119 days ago
    1
    import { LinkedIn } from "https://deno.land/x/linkedin/mod.ts";
    2
    
    
    3
    // Your LinkedIn credentials
    4
    
    
    5
    
    
    6
    // The image and caption you want to post
    7
    const imageFilePath = "/path/to/your/image.jpg";
    8
    const caption = "Your caption goes here.";
    9
    
    
    10
    // Log in to LinkedIn
    11
    const li = await LinkedIn.login({ email, password });
    12
    
    
    13
    // Upload the image to LinkedIn
    14
    const image = await li.uploadImage(imageFilePath);
    15
    
    
    16
    // Post the image with the caption
    17
    await li.postShare({ image, caption });
    18
    
    
    19
    // Log out of LinkedIn
    20
    await li.logout();
    21
    
    
  • Submitted by adam186 Deno
    Created 395 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