3

我在使用 PHP 的 PECL OAuth 模块向 LinkedIn 共享 API 发出请求时遇到问题。我已经用谷歌搜索了我的价值,但我找不到任何例子。

我的 PHP 代码:

$linkedin_oauth = new OAuth($linkedin_consumer_key,$linkedin_consumer_secret,OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
  $linkedin_oauth->setToken($linkedin_data->linkedin_token, $linkedin_data->linkedin_consumer_secret);
  $linkedin_oauth->setAuthType(OAUTH_AUTH_TYPE_FORM);

  $body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  $body .= "<share>";
  $body .= "<comment>83% of employers will use social media to hire: 78% LinkedIn, 55% Facebook, 45% Twitter [SF Biz Times] http://bit.ly/cCpeOD</comment>";
  $body .= "<content>";
  $body .= "<title>Survey: Social networks top hiring tool - San Francisco Business Times</title>";
  $body .= "<submitted-url>http://sanfrancisco.bizjournals.com/sanfrancisco/stories/2010/06/28/daily34.html</submitted-url>";
  $body .= "<submitted-image-url>http://images.bizjournals.com/travel/cityscapes/thumbs/sm_sanfrancisco.jpg</submitted-image-url>";
  $body .= "</content>";
  $body .= "<visibility>";
  $body .= "<code>anyone</code>";
  $body .= "</visibility>";
  $body .= "</share>";

$headers = array('Content-Type' => 'text/xml');
  $linkedin_oauth->fetch("http://api.linkedin.com/v1/people/~/shares", $body, OAUTH_HTTP_METHOD_POST, $headers);
  $linkedin_share_response = $linkedin_oauth->getLastResponse();

但这会导致异常:

Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)

我的标题看起来像:

OAuth oauth_consumer_key="<KEY_REMOVED>",oauth_signature_method="HMAC-SHA1",oauth_nonce="12581672284fa32618a44a31.27944550",oauth_timestamp="1336092184",oauth_version="1.0",oauth_token="<TOKEN_REMOVED>",oauth_signature="Qqt5zQKITSvk7OyrdWdFLaR%2F9ns%3D

有谁知道这里发生了什么?无论我做什么,响应始终是 401。

当我用 GET 调用替换 call 时,它工作正常。

4

2 回答 2

0

网上似乎没有很多关于这些东西的信息,但这似乎对我有用。如果您使用 OAuth2.0 访问令牌,这应该可以工作。插入并插入您的变量,如果您有任何问题,请告诉我。

$params = array(
    'oauth2_access_token'   => $oauth2_access_token
);

$url = 'https://api.linkedin.com/v1/people/~/shares?' . http_build_query($params);

$xml_content = '<?xml version="1.0" encoding="UTF-8"?><share><content><title>' . $title . '</title><submitted-url>' . $link . '</submitted-url><submitted-image-url>' . $image . '</submitted-image-url></content><visibility><code>anyone</code></visibility></share>';


$context = stream_context_create(array(
    'http' => array(
        'method'    => 'POST',
        'content'   => $xml_content,
        'header'    => "Content-type: application/xml"
    )
));

$stream = fopen($url, 'rb', false, $context);

if ($stream) {
    $metaData = stream_get_meta_data($stream);
    $response = stream_get_contents($stream);
    fclose($stream);
}
于 2014-02-18T11:17:16.223 回答
0

检查您注册的 OAuth 应用程序中的返回 URL。它必须是您的脚本的 URL。

于 2013-02-04T10:01:50.547 回答