linkedin-apilinkedin-jsapi

How to add Comment on linkedin company page feeds using api


I'm new to working with Linkedin API.

I'm trying to add a comment on the company page feeds using API curl.

Here is my code.

$params1 = array(
    "endpoint"  => "https://api.linkedin.com/v2/socialActions/urn:li:activity:********/comments",
    "body"  => array(
        'actor' => 'urn:li:person:********',
        'object' => 'urn:li:activity:*********',
        'message' => array(
            'text' => 'test..........11'
        ),
        'content' => array()
    ),
);

$lin_access_token = get_user_meta(get_current_user_id(), 'lin_access_token', true);

$response1 = wp_remote_post($params1['endpoint'], array(
    'method'      => 'POST',
    'body'        => json_encode($params1['body']),
    'headers'     => array(
        'Authorization' => 'Bearer ' . $lin_access_token,
        'x-Restli-Protocol-Version' => '2.0.0',
        'Content-Type' => 'application/json',
    ),

));

$responseBody1 = wp_remote_retrieve_body($response1);
$result1 = json_decode($responseBody1, true);
ravi_debug($result1);

I don't know what I'm doing wrong in this. I'm getting the following error with try.

Array
(
    [serviceErrorCode] => 0
    [message] => Syntax exception in path variables
    [status] => 400
)

I have tried this in postman I'm getting the proper output.


Solution

  • The socialActions endpoint require URN of type share or ugcPost instead of activity:

    POST https://api.linkedin.com/v2/socialActions/{shareUrn|ugcPostUrn}/comments
    

    So try to use an URN of format urn:li:share:{id} or urn:li:ugcPost:{id}.

    NB: You could use the Activities API in order to retrieve the share URN, as example:

    https://api.linkedin.com/v2/activities?ids=urn:li:activity:6252923622642540544
    

    will return:

    {
        "errors": {},
        "results": {
            "urn:li:activity:6252923622642540544": {
                "domainEntity": "urn:li:share:6252923622768377856"
            }
        },
        "statuses": {}
    }