Search code examples
linkedin-api

Mentions in LinkedIn comments do not work via API


When trying to mention a user, an error is returned: API endpoint docs

What I do:

POST https://api.linkedin.com/rest/socialActions/urn%3Ali%3Ashare%3A%7BshareId%7D/comments

Request body:

{
    "actor": "urn:li:organization:{actorId}",
    "object": "urn:li:share:{shareId}",
    "message": {
        "attributes": [
            {
                "length": 14,
                "start": 0,
                "value": {
                    "com.linkedin.adsexternalapi.social.PersonAttributedEntity": {
                        "person": "urn:li:person:{personId}"
                    }
                }
            }
        ],
        "text": "Mentioned user test message!"
    }
}

Response:

{
    "message": "ERROR :: /message/attributes/0/value/com.linkedin.adsexternalapi.social.PersonAttributedEntity :: unrecognized field found but not allowed\nERROR :: /message/attributes/0/value :: \"com.linkedin.adsexternalapi.social.PersonAttributedEntity\" is not a member type of union",
    "status": 422
}

The request is made by analogy with what is indicated in the example.


Solution

  • The documentation appears to be wrong...

    I was able to get comment mentions working by using:

    {
        "actor": "urn:li:organization:{actorId}",
        "object": "urn:li:share:{shareId}",
        "message": {
            "attributes": [
                {
                    "length": 14,
                    "start": 0,
                    "value": {
                        "com.linkedin.common.MemberAttributedEntity": {
                            "person": "urn:li:person:{personId}"
                        }
                    }
                }
            ],
            "text": "Mentioned user test message!"
        }
    }
    

    I searched for some older docs and that solved the issue.

    Value attributes are com.linkedin.common.CompanyAttributedEntity and com.linkedin.common.MemberAttributedEntity

    More info: https://learn.microsoft.com/en-us/linkedin/compliance/integrations/shares/ugc-post-api?tabs=http#mentions-in-ugc-posts

    Here's my entire funtion:

    export const createComment = async (
      accessToken: string,
      postUrn: string,
      commentParams: CommentMutation,
    ): Promise<string | undefined> => {
      return (
        await axios.post(
          `https://api.linkedin.com/v2/socialActions/${postUrn}/comments`,
          {
            ...commentParams,
          },
          {
            headers: {
              Authorization: `Bearer ${accessToken}`,
              "Content-Type": "application/json",
            },
          },
        )
      ).data;
    };