1

I want to get the profile info after getting the token

<?php

    session_start ();

    if (!$_SESSION['linkedin_access_token'])
    {
        echo 'Grant access first';
        exit ();
    }

    ## step 0
    define ('LINKEDIN_KEY', 'xxxxxx');
    define ('LINKEDIN_SECRET', 'xxxxxxx');

    function urlencode_oauth ($str)
    {
        return str_replace ('+', ' ', str_replace ('%7E', '~', rawurlencode ($str)));
    }

    $links = array (
        'request_token' => 'https://api.linkedin.com/uas/oauth/requestToken',
        'authorize' => 'https://www.linkedin.com/uas/oauth/authorize',
        'access_token' => 'https://api.linkedin.com/uas/oauth/accessToken',
    );


    ## step 2
    $params = array (
        'oauth_consumer_key' => LINKEDIN_KEY,
        'oauth_nonce' => sha1 (microtime ()),
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_timestamp' => time (),
        'oauth_token' => $_SESSION ['linkedin_access_token'],
        'oauth_version' => '1.0'
    );

    ## step 3
    // sort parameters according to ascending order of key
    // sort parameters according to ascending order of key
    ksort ($params);

    // prepare URL-encoded query string
    $q = array ();
    foreach ($params as $key => $value)
    {
        $q [] = urlencode_oauth ($key) . '=' . urlencode_oauth ($value);
    }
    $q = implode ('&', $q);

    // generate the base string for signature
    $parts = array (
        'POST',
        urlencode_oauth ('https://api.linkedin.com/v1/people/~'),
        urlencode_oauth ($q)
    );
    $base_string = implode ('&', $parts);

    ## step 4
    $key = urlencode_oauth (LINKEDIN_SECRET) . '&' . urlencode_oauth ($_SESSION ['linkedin_access_token_secret']);
    $signature = base64_encode (hash_hmac ('sha1', $base_string, $key, true));

    ## step 5
    $params ['oauth_signature'] = $signature;
    $str = array ();
    foreach ($params as $key => $value)
    {
        $str [] = $key . '="' . urlencode_oauth ($value) . '"';
    }
    $str = implode(', ', $str);
    $headers = array (
        'POST /v1/people/~ HTTP/1.1',
        'Host: api.linkedin.com',
        'Authorization: OAuth ' . $str,
        'Content-Type: text/xml;charset=UTF-8',
        'Content-Length: 0',
        'Connection: close'
    );

    ## step 6
    $fp = fsockopen ("ssl://api.linkedin.com", 443, $errno, $errstr, 30);
    if (!$fp)
    {
        echo 'Unable to connect to LinkedIn';
        exit();
    }
    $out = implode ("\r\n", $headers) . "\r\n\r\n";
    fputs ($fp, $out);

    // getting LinkedIn server response
    $res = '';
    while (!feof ($fp)) $res .= fgets ($fp, 4096);
    fclose ($fp);

    echo '<pre>';
    echo $res . "\n\n";
    echo $_SESSION ['linkedin_access_token'] . "\n" . $_SESSION ['linkedin_access_token_secret'];

 ?>

What's wrong with it? it shows me

HTTP/1.1 405 Method Not Allowed
Server: Apache-Coyote/1.1
x-li-request-id: H8M76QXW5O
Date: Tue, 04 Sep 2012 12:09:21 GMT
Vary: *
x-li-format: xml
Content-Type: text/xml;charset=UTF-8
Content-Length: 262

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
  <status>405</status>
  <timestamp>1346760561727</timestamp>
  <request-id>H8M76QXW5O</request-id>
  <error-code>0</error-code>
  <message>Unsupported POST target {/v1/people/~}</message>
</error>


xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx

2 Answers 2

2
define('LINKEDIN_KEY', 'YOUR_KEY');
define('LINKEDIN_SECRET', 'YOUR SECRET');

function urlencode_oauth($str) {
return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str)));
}
$links = array(
 'request_token'=>'https://api.linkedin.com/uas/oauth/requestToken',
  'authorize'=>'https://www.linkedin.com/uas/oauth/authorize',
  'access_token'=>'https://api.linkedin.com/uas/oauth/accessToken'
);
$params = array(
  'oauth_callback'=>"YOUR CALLBACK URL",
  'oauth_consumer_key'=>LINKEDIN_KEY,
  'oauth_nonce'=>sha1(microtime()),
  'oauth_signature_method'=>'HMAC-SHA1',
  'oauth_timestamp'=>time(),
  'oauth_version'=>'1.0'
);
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'POST',
      urlencode_oauth($links['request_token']),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&';
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'POST /uas/oauth/requestToken HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Content-Type: text/xml;charset=UTF-8',
      'Content-Length: 0',
      'Connection: close'
    );
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers) . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);

    $parts = explode("\n\n",str_replace("\r",'',$res));
    $res_headers = explode("\n",$parts[0]);
    if ($res_headers[0] != 'HTTP/1.1 200 OK') {
      echo 'Error getting OAuth token and secret.'; exit();
    }
    parse_str($parts[1],$data);
    if (empty($data['oauth_token'])) {
      echo 'Failed to get LinkedIn request token.'; exit();
    }

Replace three things 1. 'YOUR_KEY' 2. 'YOUR_SECRET' and 3.'oauth_callback'.

Thanks,

Anand

Sign up to request clarification or add additional context in comments.

1 Comment

Please follow this and just replace three things by your own 'LINKEDIN_KEY','LINKEDIN_SECRET' AND 'oauth_callback'
1

As the documentation indicates, the Profile API does not support the POST method. Try using GET instead to retrieve profile data.

4 Comments

writing GET in place of POST showed: HTTP/1.1 401 Unauthorized Server: Apache-Coyote/1.1 x-li-request-id: KN3IEFYYXD Date: Tue, 04 Sep 2012 16:24:19 GMT Vary: * x-li-format: xml Content-Type: text/xml;charset=UTF-8 Content-Length: 341 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <error> <status>401</status> <timestamp>1346775860118</timestamp> <request-id>KN3IEFYYXD</request-id> <error-code>0</error-code> <message>[unauthorized]. OAU:jyedyo3jrxcq|64d50e16-2e22-4d76-a01b-cca5e5fdf073|*01|*01:1346775859:yUgqbX10RoGb8loldCSRLRFeNPg=</message> </error>
GET is the correct method - the error you are getting seems to indicate a problem with the OAuth itself - try the OAuth console to ensure that everything works with your keys and that it is an OAuth issue on your end
Wow, worked well. I only changer POST in 1 place, changing in 2 places worked like a charm. Thanks a lot
once again when I tried api.linkedin.com/v1/people/… then the code didn't work, pls any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.