LinkedIn REST API - 内部服务器错误

LinkedIn REST API - Internal server error

我正在使用 LinkedIn REST API 到 post 更新用户时间线。 几天后,我收到了 LinkedIn 的 Internal server error 回复,但代码之前有效。

PHP:

$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";

$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'
);

curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

如何修复该错误?

您的代码无效 PHP(可能是因为您在发布前进行了一些编辑?);修改为:

$postTitle = "hello";
$postDesc = "world ";
$postURL = "http://example.com";
$postImage = "http://images.example.com/img/hello.jpg";
$postComment = "foobar";

$oauthToken = "<token>";

$postData = array(
    'visibility' => array('code' => 'connections-only'),
    'content' => array(
        'title' => $postTitle,
        'description' => $postDesc,
        'submitted-url' => $postURL,
        'submitted-image-url' => $postImage
    ),
    'comment' => $postComment
);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json');

curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

仅在 $oauthToken 设置为有效令牌时才有效。假设您的真实代码是正确的,剩下的唯一可能是您的 OAuth 令牌已过期,您需要先获取一个新的。通过将 CURLOPT_VERBOSE => TRUE 添加到 cURL 选项,您会发现有关 LinkedIn returns.

错误的更多信息

您可以考虑使用 LinkedIn PHP SDK(由社区提供):https://github.com/Happyr/LinkedIn-API-client

我们最近在 Linkedin API 中遇到了类似的问题。最后通过更改 url 找到了解决方法。

新 URL:“https://api.linkedin.com/v1/people/~/shares

而不是在查询字符串中指定 'oauth2_access_token', 将其添加到 header - 指定:

"Authorization", "Bearer " + accessToken.

最后在请求 body 参数中,将您的 json/xml 数据添加到 post

您必须在请求中使用您的身份验证令牌 Headers。

这是工作代码。试试吧。

$postTitle = "hello";
$postDesc = "world ";
$submitted-url = "http://example.com";
$submitted-image-url = "http://images.example.com/img/hello.jpg";
$comment = "foobar";

$oauthToken = "TokenHere";


$postData = array('visibility' => array('code' => 'connections-only'),
'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment);

$ch = curl_init('https://api.linkedin.com/v1/people/~/shares?format=json');


curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json", "Bearer: ".$oauthToken.""),
CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);