通过 Rest API 在 LinkedIn 页面上发布文章

问题描述 投票:0回答:1

我想以编程方式将文章发布到我的 LinkedIn 页面上,类似于我手动执行的操作。 查看这里

3 年零 2 个月前有人问过类似的问题,但问题是关于在 LinkedIn 上分享一篇博客文章。 这是我指的主题。

这是我到目前为止所尝试过的(两次它都只是与页面上的文本共享外部 URL,而不是将其创建为一篇文章。我知道这是因为它就是用于此目的。但我想在我的网站上创建一篇文章LinkedIn 页面就像我手动创建的一样。

有人知道 LinkedIn 是否有用于在 linkdein 页面上创建和发布文章的 API?

发布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'))
            ],
        ]);

我也尝试过这个:

发布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
    ]);
php laravel linkedin-api social-networking
1个回答
0
投票

我在 6 个月前使用过这段代码,它有效,希望它有帮助

    <?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);
?>
© www.soinside.com 2019 - 2024. All rights reserved.