LinkedIn oAuth - 访问简单的只读公共数据 - 没有发布等

时间:2011-09-20 15:21:25

标签: php oauth linkedin

我正在尝试对LinkedIn进行非常基本的设置 - 访问有关人员的公开个人资料数据。

我已经尝试了几种方式使用oAuth访问几个小时。我没有到达任何地方。

所有现有的类结构似乎都有助于访问用户的帐户发布,或添加朋友,找到鱼等。我不需要任何这些。我只想获得基本的个人资料数据。

最新尝试的一些代码;但我不知道它在这里: -

$consumer = new OAuthConsumer($apiKey, $apiSecret);
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$req_req = OAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $linkedInURL . "/uas/oauth/requestToken");
$req_req->sign_request($signature_method, $consumer, NULL);         
$signed_url = $req_req->to_url();   

这应该给我一个签名的请求: -

https://api.linkedin.com/uas/oauth/requestToken?oauth_consumer_key=xxx&oauth_nonce=xx&oauth_signature=xxxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1316530337&oauth_version=1.0

显然,这不是我获取数据所需要的。但是,出于兴趣,我使用API​​数据请求检查了URL,如下: -     http://api.linkedin.com/v1/people/url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fchrisvoss:public?oauth_consumer_key=xxx&oauth_nonce=xxx&oauth_signature=xxx&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1316529463&oauth_version=1.0

得到了: -

<error>
 <status>401</status>
  <timestamp>1316531835564</timestamp>
  <request-id>L8A1M85MWN</request-id>
  <error-code>0</error-code>
  <message>
        [unauthorized].OAU:tm6i3ke827xz|*01|*01|*01:1316529463:MSFHS3f4iaG9pg2gWYlf22W4NPo=
     </message>
</error>

我在这里有点无能为力。我知道每个人都说实施oAuth和Linkedin是泪流满面。但是,我不需要最需要的一半,所以我如何获得基本数据只是我的问题。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

Try To Use the following code

   session_start();
require_once("OAuth.php");  



$domain = "https://api.linkedin.com/uas/oauth";
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();



$test_consumer = new OAuthConsumer("DEHYU99peS88wDDAFOcSm3Af5VO1tdrdgq1xPu_fpSSjsqPcoeABUs_NCyY33WIH", "gZrZr2-7s80CEsGpAHqFgREMbRWkR3L8__tkje3j-oKtIDlmn5KCR6bXD8i0HFp1", NULL);
$callback = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?action=getaccesstoken";



# First time through, get a request token from LinkedIn.
if (!isset($_GET['action'])) {

        $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "POST", $domain . "/requestToken");
        $req_req->set_parameter("oauth_callback", $callback); # part of OAuth 1.0a - callback now in requestToken
        $req_req->sign_request($sig_method, $test_consumer, NULL);

        $ch = curl_init();
        // make sure we submit this as a post
        curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array (
                $req_req->to_header()
        ));
        curl_setopt($ch, CURLOPT_URL, $domain . "/requestToken");
        curl_setopt($ch, CURLOPT_POST, 1);
        $output = curl_exec($ch);
        curl_close($ch);

        //print_r($req_req);  //<---- add this line

        //print("$output\n");  //<---- add this line

        parse_str($output, $oauth);


        # pop these in the session for now - there's probably a more secure way of doing this! We'll need them when the callback is called.

        $_SESSION['oauth_token'] = $oauth['oauth_token'];
        $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];



        # Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
        Header('Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']);
        #print 'Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']; // <---- add this line


} else {
        # this is called when the callback is invoked. At this stage, the user has authorised the token.
        # Now use this token to get a real session token!

        //print "oauth_token = [[".$_REQUEST['oauth_token']."]]\n";echo "<br/><br/>";

        $req_token = new OAuthConsumer($_REQUEST['oauth_token'], $_SESSION['oauth_token_secret'], 1);
        $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "POST", $domain . '/accessToken');
        $acc_req->set_parameter("oauth_verifier", $_REQUEST['oauth_verifier']);  # need the verifier too!
        $acc_req->sign_request($sig_method, $test_consumer, $req_token);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array (
                $acc_req->to_header()
        ));
        curl_setopt($ch, CURLOPT_URL, $domain . "/accessToken");
        curl_setopt($ch, CURLOPT_POST, 1);
        $output = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Curl error 1: ' . curl_error($ch);
        }
        curl_close($ch);
        parse_str($output, $oauth);


        $_SESSION['oauth_token'] = $oauth['oauth_token'];
        $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
        # Now you have a session token and secret. Store these for future use. When the token fails, repeat the above process.
        //$endpoint = "http://in.linkedin.com/in/intercom"; # need a + symbol here.
        $endpoint = "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,industry,educations,site-standard-profile-request)";
        //$req_token = new OAuthConsumer($oauth['oauth_token'], $oauth['oauth_token_secret'], 1);
        $req_token = new OAuthConsumer($oauth['oauth_token'],$oauth['oauth_token_secret'], 1);
        //$profile_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "GET", $endpoint, array("name" => "intercom")); # but no + symbol here!
        $profile_req = OAuthRequest::from_consumer_and_token($test_consumer,$req_token, "GET", $endpoint, array());
        $profile_req->sign_request($sig_method, $test_consumer, $req_token);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array (
                $profile_req->to_header()
        ));
        curl_setopt($ch, CURLOPT_URL, $endpoint);
        $output = curl_exec($ch);


        if(curl_errno($ch)){
            echo 'Curl error 2: ' . curl_error($ch);
        }
        curl_close($ch);
        //header ("Content-Type:text/xml");  
        //print $output;

        $myFile = $_SERVER['DOCUMENT_ROOT']."/oauth/linkedin.xml";
        $fh = fopen($myFile, 'w') or die("can't open file");
        //$stringData = "Bobby Bopper\n";
        fwrite($fh, $output);
        fclose($fh);

        //Initialize the XML parser
        global $currentTag;
        global $profileArray;
        $parser=xml_parser_create();

        //Function to use at the start of an element
        function start($parser,$element_name,$element_attrs) {
            $element_name   =   strtolower($element_name);
            global $currentTag;
            $currentTag = $element_name;
            /*switch($element_name) {
                case "person":
                $currentTag = $element_name;
                break;
                case "headline":
                echo "headline: ";
                break;
                case "school-name":
                echo "school-name: ";
                break;
                case "degree":
                echo "degree: ";
                break;
                case "field-of-study":
                echo "field-of-study: ";
            }*/
        }

        //Function to use at the end of an element
        function stop($parser,$element_name) {}

        //Function to use when finding character data
        function char($parser,$data){

            //echo $data;
            global $currentTag;
            global $profileArray;
            switch($currentTag) {

                /* case "member-url":
                if(!isset($profileArray['member-url'])) {
                    $profileArray['member-url'] =   $data;//echo $profileArray['industry'];
                }
                break;*/
                 case "id":
                if(!isset($profileArray['id'])) {
                    $profileArray['id'] =   $data;//echo $profileArray['industry'];
                }
                break;

                 case "site-standard-profile-request":
                if(!isset($profileArray['site-standard-profile-request'])) {
                    $profileArray['site-standard-profile-request']  =   $data;//echo $profileArray['industry'];
                }
                break;
                case "first-name":
                if(!isset($profileArray['first-name'])) {
                    $profileArray['first-name'] =   $data;//echo $profileArray['industry'];
                }
                break;

                case "last-name":
                if(!isset($profileArray['last-name'])) {
                    $profileArray['last-name']  =   $data;//echo $profileArray['industry'];
                }
                break;
                case "industry":
                if(!isset($profileArray['industry'])) {
                    $profileArray['industry']   =   $data;//echo $profileArray['industry'];
                }
                break;
                case "headline":
                if(!isset($profileArray['headline'])) {
                    $profileArray['headline']   =   $data;
                }   
                break;
                case "school-name":
                if(!isset($profileArray['school-name'])) {
                    $profileArray['school-name']    =   $data;
                }
                break;
                case "degree":
                if(!isset($profileArray['degree'])) {
                    $profileArray['degree'] =   $data;
                }
                break;
                case "field-of-study":
                if(!isset($profileArray['field-of-study'])) {
                    $profileArray['field-of-study'] =   $data;
                }
                break;
            }
        }

        //Specify element handler
        xml_set_element_handler($parser,"start","stop");

        //Specify data handler
        xml_set_character_data_handler($parser,"char");

        //Open XML file
        $fp=fopen($_SERVER['DOCUMENT_ROOT']."/oauth/linkedin.xml","r");

        //Read data
        while ($data=fread($fp,4096)) {
            xml_parse($parser,$data,feof($fp)) or
            die (sprintf("XML Error: %s at line %d",
            xml_error_string(xml_get_error_code($parser)),
            xml_get_current_line_number($parser)));
        }

        //Free the XML parser
        xml_parser_free($parser);
        print_r($profileArray);
        getCurrentCookieValue($name)

}

答案 1 :(得分:0)

您可以使用linkedIn javascript API来检索个人资料信息