0

It's been a week and I cannot post to the company page. The below code works but it's sending to my personal profile instead of the company page. Anyone can advise, please?

<?php
require_once '../vendor/autoload.php';
use GuzzleHttp\Client;
  
$link = 'www.google.com';
$access_token = 'MY_ACCESS_TOKEN';
$linkedin_id = 'EWg7hNHQ9F';

$body = new \stdClass();
$body->content = new \stdClass();
$body->content->contentEntities[0] = new \stdClass();
$body->text = new \stdClass();
$body->content->contentEntities[0]->thumbnails[0] = new \stdClass();
$body->content->contentEntities[0]->entityLocation = $link;
$body->content->contentEntities[0]->thumbnails[0]->resolvedUrl = "https://picsum.photos/200/300";
$body->content->title = 'Testing Only';
$body->owner = 'urn:li:person:'.urlencode($linkedin_id);
$body->text->text = 'Testing summary';
$body_json = json_encode($body, true);
  
try {
    $client = new Client(['base_uri' => 'https://api.linkedin.com']);
    $response = $client->request('POST', '/v2/shares', [
    // $response = $client->request('POST', '/v2/ugcPosts', [
        'headers' => [
            'X-Restli-Protocol-Version' => '2.0.0',
            "Authorization" => "Bearer " . $access_token,
            "Content-Type"  => "application/json",
            // "x-li-format"   => "json"
        ],
        'body' => $body_json,
    ]);
  
    if ($response->getStatusCode() !== 201) {
        echo 'Error: '. $response->getLastBody()->errors[0]->message;
    }
  
    echo 'Post is shared on LinkedIn successfully.';
} catch(Exception $e) {
    echo $e->getMessage(). ' for link '. $link;
}

1 Answers1

0

I just skimmed the API docs, Maybe your URN is wrong?

urn:li:organization Organization Share Share was created by a member for an organizational entity. The organization or brand is displayed as the publisher.

Source: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/share-api?tabs=http

Other Reference: Post an article to company LinkedIN page via REST api (v2)

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
ghgg
  • 106
  • 3
  • Thank you @ghgg, I tried to put `$body->owner = 'urn:li:organization:123456;` But then I got the following error Client error: `POST https://api.linkedin.com/v2/shares` resulted in a `400 Bad Request` response: {"message":"com.linkedin.content.common.exception.BadRequestResponseException: Company owned UGCs cannot have CONNECTION (truncated...) – user1668975 Dec 29 '21 at 06:43
  • Could you post the full error message? – ghgg Dec 29 '21 at 06:53
  • Client error: `POST https://api.linkedin.com/v2/ugcPosts` resulted in a `403 Forbidden` response: {"serviceErrorCode":100,"message":"Unpermitted fields present in REQUEST_BODY: Data Processing Exception while processin (truncated...) for link google.com – user1668975 Dec 29 '21 at 07:16
  • To me, it seems those are two different errors? The 403 Forbidden stands out, you might not have the correct permissions. try looking here for the 403 error: https://learn.microsoft.com/en-us/linkedin/shared/api-guide/concepts/error-handling – ghgg Dec 29 '21 at 07:23
  • Another hint, Unpermitted fields in REQUEST_BODY it says in your error, I would check your body_json and see if something is messing you up in that too. – ghgg Dec 29 '21 at 07:39
  • I tried a lot and nothing seems to work. I checked the permissions and body and everything. I'll try a different approach. Do you know any sample codes resources for linkedIn? – user1668975 Dec 29 '21 at 08:43
  • I don't, I've never used the API, I would suggest going through the docs. I also suggest changing the body up and seeing if that changes anything. It's strange that you can post to your personal, but not the company page. There has to be a reason buried in the docs somewhere. Look at other examples of posting to company pages with the Linkedin API regardless of whether it's in PHP or not. I looked up "linkedin api example code post to company page" found this among others: https://www.jcchouinard.com/how-to-post-on-linkedin-api-with-python/ Sorry I don't have a definite answer for you – ghgg Dec 29 '21 at 09:12
  • if/when you figure it out please update this question with what worked :) – ghgg Dec 29 '21 at 09:13
  • I will keep trying and post an update if I find a solution. Thank you so much for all the effort :) – user1668975 Dec 29 '21 at 10:23