使用cURL和 PHP将内容分享到LinkedIn——身份验证失败
我正在开发一个用PHP编写的应用,目标是在LinkedIn页面上发帖。首先,我的应用会重定向到授权URL(https://www.linkedin.com/oauth/v2/authorization)以获取在LinkedIn账户上发帖的权限(测试时我使用的是我的个人账户)。从中,我获得了一个访问令牌。令牌检查器显示:
Access token details
Created on: about 17 hours ago (1780442716 UTC)
Last authorized: about 17 hours ago (1780442716 UTC)
Expires: in about 2 months (1785626718 UTC)
Authentication type: 3-legged
Permissions: email, openid, profile, w_member_social
Status: OAuth token is active
为了生成分享内容,我首先使用该OAuth令牌去调用 https://api.linkedin.com/v2/userinfo,以便获得“sub”值来创建作者字符串:urn:li:person:MY-SUB-VALUE。
之后我使用文档中的示例,尝试用JSON内容向 https://api.linkedin.com/v2/ugcPosts 发送文本分享:
{
"author": "urn:li:person:MY-SUB-VALUE",
"lifecycleState": "PUBLISHED",
"specificContent": {
"com.linkedin.ugc.ShareContent": {
"shareCommentary": {
"text": "Hello World! This is my first Share on LinkedIn!"
},
"shareMediaCategory": "NONE"
}
},
"visibility": {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
}
}
以及头信息:
$header_list = [
"X-Restli-Protocol-Version: 2.0.0",
"Authorization: Bearer ACCESS_TOKEN",
];
使用这段PHP curl代码:
$handle = curl_init();
$curl_options = [
CURLOPT_URL => "https://api.linkedin.com/v2/ugcPosts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $header_list;
CURLOPT_POSTFIELDS => $content;
];
curl_setopt_array($handle, $curl_options);
$raw_data = curl_exec($handle);
这返回一个(HTML页面)错误“你必须进行身份验证才能访问此页”。我认为我一切都就绪,包括w_member_social范围,那么我还缺少什么?
解决方案
没想到这一点——我用于发帖的基本URL与用于其他操作的URL不同——认证调用需要“https://www.linkedin.com/oauth/v2/”,而发帖需要“https://api.linkedin.com/v2/”。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。