Edits history of script submission #22437 for ' Create a Simple Post (User) (linkedin)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    type Linkedin = {
      token: string;
    };
    export async function main(
      auth: Linkedin,
      content: string,
      visibility: "PUBLIC" | "CONNECTIONS" = "PUBLIC",
    ) {
      const entityResponse = await fetch("https://api.linkedin.com/v2/me", {
        headers: { Authorization: "Bearer " + auth.token },
      });
      const entityId = (await entityResponse.json()).id;
    
      const url = new URL("https://api.linkedin.com/v2/ugcPosts");
      const body = JSON.stringify({
        author: `urn:li:person:${entityId}`,
        lifecycleState: "PUBLISHED",
        specificContent: {
          "com.linkedin.ugc.ShareContent": {
            shareCommentary: {
              text: content,
            },
            shareMediaCategory: "NONE",
          },
        },
        visibility: {
          "com.linkedin.ugc.MemberNetworkVisibility": visibility,
        },
      });
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: "Bearer " + auth.token,
          "X-Restli-Protocol-Version": "2.0.0",
        },
        body,
      });
    
      if (!response.ok) {
        throw Error(await response.text());
      }
      return await response.json();
    }
    

    Submitted by hugo989 3 days ago