Post Article on LinkedIn Page by Rest API

454 Views Asked by At

I want to programmatically post the article on my LinkedIn page similar to how I manually did. Check here

A similar question was asked 3 years and 2 months ago but it's about sharing a blog article on LinkedIn. Here is the thread I'm referring.

This is what I have tried so far (both times it just shares the external URL with text on the page instead of creating this as an article. I know that's because it's intended for that purpose. But I want to create an article on my LinkedIn page like how I manually created one.

Anyone knows is LinkedIn has API for creating and posting article on linkdein page?

POST https://api.linkedin.com/v2/ugcPosts

$media = [
        'status' => 'READY',
        'originalUrl' => $originalUrl
    ];

    if ($title !== '') {
        $media['title'] = ['text' => $title];
    }

    if ($description !== '') {
        $media['description'] = ['text' => $description];
    }

    $response = $this->getHttpClient()::withToken($this->getAccessToken()['access_token'])
        ->withHeaders($this->httpHeaders())
        ->post("$this->apiUrl/$this->apiVersion/ugcPosts", [
            'author' => "urn:li:{$this->author()}:{$this->values['provider_id']}",
            'lifecycleState' => 'PUBLISHED',
            'specificContent' => [
                'com.linkedin.ugc.ShareContent' => [
                    'shareCommentary' => [
                        'text' => $commentaryText,
                    ],
                    'shareMediaCategory' => 'ARTICLE',
                    'media' => [$media]
                ]
            ],
            'visibility' => [
                'com.linkedin.ugc.MemberNetworkVisibility' => Str::upper(Arr::get($params, 'visibility', 'PUBLIC'))
            ],
        ]);

I also tried this:

POST https://api.linkedin.com/rest/posts

$articleContent = [
    'article' => [
        'source' => $articleSource,
        'thumbnail' => $thumbnailUrn,
        'title' => $title,
        'description' => $description
    ]
];

$response = $this->getHttpClient()::withToken($this->getAccessToken()['access_token'])
    ->withHeaders($this->httpHeaders())
    ->post("https://api.linkedin.com/rest/posts", [
        'author' => "urn:li:organization:{$this->values['provider_id']}",
        'commentary' => $commentary,
        'visibility' => Str::upper(Arr::get($params, 'visibility', 'PUBLIC')),
        'distribution' => [
            'feedDistribution' => 'MAIN_FEED',
            'targetEntities' => [],
            'thirdPartyDistributionChannels' => []
        ],
        'content' => $articleContent,
        'lifecycleState' => 'PUBLISHED',
        'isReshareDisabledByAuthor' => false
    ]);
1

There are 1 best solutions below

2
TSCAmerica.com On

I used this code like 6 months ago, it worked, hope it helps

    <?php
$accessToken = 'YOUR_ACCESS_TOKEN'; 
$organizationId = 'YOUR_ORGANIZATION_ID';

$articleContent = [
    'author' => "urn:li:organization:$organizationId",
    'commentary' => 'Your article commentary here',
    'visibility' => 'PUBLIC',
    'lifecycleState' => 'PUBLISHED',
    'specificContent' => [
        'com.linkedin.ugc.ShareContent' => [
            'shareCommentary' => [
                'text' => 'Introductory text for your article.'
            ],
            'shareMediaCategory' => 'ARTICLE',
            'media' => [
                [
                    'status' => 'READY',
                    'description' => [
                        'text' => 'Your article description'
                    ],
                    'title' => [
                        'text' => 'Your article title'
                    ],
                    'originalUrl' => 'https://your-article-source-link.com',
                    // Use this field to add the content of your article
                    'article' => [
                        'text' => 'The full text of your article goes here...'
                    ]
                ]
            ]
        ]
    ],
    'visibility' => [
        'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
    ]
];

$headers = [
    'Authorization: Bearer ' . $accessToken,
    'X-Restli-Protocol-Version: 2.0.0',
    'LinkedIn-Version: YYYYMM', 
    'Content-Type: application/json'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.linkedin.com/rest/posts');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($articleContent));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    echo 'Response:' . $response;
}
curl_close($ch);
?>