2

I am trying to make an API to à LinkedIn users profile using PHP. I've successfully registered my application and I've noted my API and Secret Key as well as listing my redirect url.

The user starts on this page: index.php. This page contains a link to the linkedIn dialog box:

<a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=<?php echo $api_key ?>&state=<?php echo $state ?>&redirect_uri=<?php echo $redirect_uri ?>">Apply Now</a>

When I click on this link I sign in to LinkedIn using my details and I am successfully redirected to application_form.php. From here I would now like to get the users profile details:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.linkedin.com/v1/people/~");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);

However the above code results in this being output:

"401 Unknown authentication scheme"

After doing a bit of research I think it might be because I still have not acquired an access token at this point? Would anyone have any idea what I should do in order to fix this?

Kara
  • 6,115
  • 16
  • 50
  • 57
Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • I don’t know if you have acquired an access token yet – but you are certainly not _using_ one in your cURL request. – CBroe Jan 21 '15 at 17:25
  • @CBroe I do have an `access_token` I can see it in the url `code=MY_ACCESS_TOKEN`. I guess I'm unsure how I can use it in my cURL request. – Javacadabra Jan 21 '15 at 17:26
  • They have a lot of PHP code samples in their documentation, so I’d suggest you go have a look there. – CBroe Jan 21 '15 at 17:28
  • I've been looking there but the code the example they provide seems to be aquiring the token by by exchanging the authorization_code for it. I am trying to Generate Authorization Code by redirecting user to LinkedIn's authorization dialog which they do not provide an example of from what I can see.... – Javacadabra Jan 21 '15 at 17:30
  • Function `getAuthorizationCode` in the example on https://developer.linkedin.com/documents/code-samples does exactly that– build the login URL and redirect the user there … – CBroe Jan 21 '15 at 17:41

1 Answers1

1

For anyone who is reading this and would like to use the LinkedIn Profile API the solution to my problem was that I did not have a valid Access Token when I attempted to make the first request.

The first thing I did was create a link that would direct the user to LinkedIn authentication dialog box.

Next the user would then choose to approve or reject my applications request to access their profile. Regardless of their choice they are redirected to my redirect url.

From here I now have an access code which I can use to request an access token and thus make api calls.

if (isset($_GET['error'])) {
    echo $_GET['error'] . ': ' . $_GET['error_description'];
} elseif (isset($_GET['code'])) {
    getAccessToken();
    //$user = fetch('GET', '/v1/people/~:(firstName,lastName)');//get name
    //$user = fetch('GET', '/v1/people/~:(phone-numbers)');//get phone numbers
    $user = fetch('GET', '/v1/people/~:(location:(name))');//get country
    var_dump($user);
}

The getAccessToken() method that I used based on the code on the LinkedIn Developers site

https://developer.linkedin.com/documents/code-samples

 function getAccessToken() {
    $params = array(
        'grant_type' => 'authorization_code',
        'client_id' => 'MY API KEY',
        'client_secret' => 'MY SECRET KEY',
        'code' => $_GET['code'],
        'redirect_uri' => 'MY REDIRECT URL',
    );
    // Access Token request
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
    // Tell streams to make a POST request
    $context = stream_context_create(
            array('http' =>
                array('method' => 'POST',
                )
            )
    );
    // Retrieve access token information
    $response = file_get_contents($url, false, $context);
    // Native PHP object, please
    $token = json_decode($response);
    // Store access token and expiration time
    $_SESSION['access_token'] = $token->access_token; // guard this! 
    $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time
    return true;
}

Then the fetch() method, also from LinkedIn API

function fetch($method, $resource, $body = '') {
    $opts = array(
        'http' => array(
            'method' => $method,
            'header' => "Authorization: Bearer " . 
            $_SESSION['access_token'] . "\r\n" . 
            "x-li-format: json\r\n"
        )
    );
    $url = 'https://api.linkedin.com' . $resource;
    if (count($params)) {
        $url .= '?' . http_build_query($params);
    }
    $context = stream_context_create($opts);
    $response = file_get_contents($url, false, $context);
    return json_decode($response);
}

By doing the above, I had no problem making requests to the API. In fairness to Cbroe who commented above. I was missing this information. If he/she would like to leave an answer I'll gladly accept but just incase I've included my solution for anyone who runs into the issue I had.

Javacadabra
  • 5,578
  • 15
  • 84
  • 152