我遇到了使用PHP的PECL OAuth模块向LinkedIn共享API发出请求的问题。我已经用Google搜索了所有值得的信息,但我找不到任何例子。
我的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呼叫替换呼叫时,它可以正常工作。
答案 0 :(得分:0)
检查已注册的OAuth应用程序中的返回URL。它必须是您脚本的URL。
答案 1 :(得分: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);
}