如何使用授权用户的访问令牌发布到LinkedIn墙?

时间:2014-09-25 13:14:32

标签: php linkedin

PHP代码

$base_url = 'https://api.linkedin.com/v1/people/~/';
$url = $base_url . 'shares';
$xml  = '<share>';
$xml .= '<comment>' . 'test'. '</comment>';
$xml .= '<content>';
$xml .= '<title>' . 'TEST' . '</title>';
$xml .= '<submitted-url>' . 'http://www.google.com' . '</submitted-url>';
$xml .= '<description>' . 'description' . '</description>';
$xml .= '</content>';
$xml .= '<visibility><code>anyone</code></visibility>';
$xml .= '</share>';

$signature = new OAuthSignatureMethod_HMAC_SHA1();
$consumer = new OAuthConsumer('my_consumer_key', 'my_consumer_secret', NULL);

如何获得$ token和$ secret变量oauth_token和oauth_token_secret 什么时候 all 我在向用户发送授权请求时是一个访问令牌? 有没有办法可以使用访问令牌获取 oauth_token oauth_token_secret

$token = new OAuthConsumer($token, $secret, 1);
$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'POST', $url, array ());
$request->sign_request($signature, $consumer, $token);
$header = $request->to_header();
$response = _linkedin_http_request($url, $header, $xml);

/*
 * Some internal helper functions...
 */
function _linkedin_http_request($url, $header, $body = NULL) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
    curl_setopt($ch, CURLOPT_URL, $url);

    if ($body) {
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($body == 'token_request') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, '');
        }
        else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array($header, 'Content-Type: text/xml;charset=utf-8'));
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        }
    }

    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

0 个答案:

没有答案