如何获取LinkedIn转发帖子的头像与企业名称并展示?
问题排查与解决方案
1. 权限缺失问题
你当前的权限列表里缺少r_member_social或r_liteprofile权限:
- r_liteprofile:用于获取用户基础资料(姓名、头像)
- r_member_social:用于获取用户社交内容相关资料
LinkedIn API要求必须具备对应权限才能访问用户姓名和头像信息,需在应用授权时添加这两个权限,重新获取access token。
2. API请求参数格式错误
你的请求URL中,projection参数后存在多余空格,会导致API解析失败:
原代码:
$profile_url = "https://api.linkedin.com/v2/people/($person_id)?projection= (id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
修正后(移除projection=后的空格):
$profile_url = "https://api.linkedin.com/v2/people/($person_id)?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
3. 转发内容所有者类型判断缺失
你默认假设resharedContent['owner']是个人用户(格式为urn:li:person:xxx),但转发内容可能来自组织(格式为urn:li:organization:xxx),此时substr(12)提取的ID无效,且需调用组织资料接口:
$owner_urn = $post['resharedContent']['owner']; if (strpos($owner_urn, 'urn:li:person:') === 0) { $person_id = substr($owner_urn, 12); $profile_url = "https://api.linkedin.com/v2/people/($person_id)?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))"; } elseif (strpos($owner_urn, 'urn:li:organization:') === 0) { $org_id = substr($owner_urn, 18); $profile_url = "https://api.linkedin.com/v2/organizations/($org_id)?projection=(id,name,logoV2(displayImage~:playableStreams))"; // 后续解析组织名称和logo的逻辑需对应调整 }
4. 本地化名称解析局限性
原代码仅提取en_US语言的名称,若用户资料无该语言版本会返回默认值。可优化为优先取用户首选语言,或取第一个可用的本地化名称:
// 优化姓名解析逻辑 if (isset($profile_data['firstName']['localized']) && isset($profile_data['lastName']['localized'])) { $first_name = reset($profile_data['firstName']['localized']); $last_name = reset($profile_data['lastName']['localized']); $reshared_company_name = $first_name . ' ' . $last_name; } else { $reshared_company_name = 'Name not available'; }
调试建议
- 打印
wp_remote_retrieve_body($response)查看API返回的具体错误信息(如权限不足、资源不存在等) - 验证access token的有效范围,确认已包含所需权限
内容的提问来源于stack exchange,提问作者user27372080
相关产品推荐
相关产品推荐

