View Full Version : OAuth with Linkedin problem - Request Token

09-20-2011, 11:51 AM
Hi guys,

I've got the following code which has been written from scratch: http://pastebin.com/y7EFJLjf


//priv key/secret
$apikey = 'myapikey';
$secret = 'myapisecret';

//get random nonce.
$nonce = rand(1,999999999);

//init curl
$ch = curl_init();

//set the auth url.
curl_setopt($ch, CURLOPT_URL, 'http://www.linkedin.com/uas/oauth/authorize');

//posting not get
curl_setopt($ch, CURLOPT_POST, true);

$params = array(
'oauth_callback' => 'http://tools.dev.co.uk/linkedin/',
'oauth_consumer_key' => $apikey,
'oauth_nonce' => $nonce,
'oauth_timestamp' => time()

//sort the params alphabetically by key

//now bring the array together and urlencode as a string to be passed in signature.
$paramString = '';
foreach ($params as $key => $param)
$paramString .= (empty($paramString)) ? $key .'='. $param : '&' . $key .'='. $param;
$paramString = urlencode($paramString);

//get the encoded secret code
$shaSecret = hash_hmac('SHA1', $secret.'&', $apikey, true);

//this gets all the params into 1 array to be posted, it also
$full_parameters = array_merge(array('oauth_signature' => 'POST&'.urlencode('https://www.linkedin.com/uas/oauth/authorize').'&'.$paramString, 'oauth_consumer_secret' => $shaSecret), $params);

//set the curl options, return header info, postfields etc
curl_setopt($ch, CURLOPT_POSTFIELDS, $full_parameters);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

//get the content.
$thereturnvalue = curl_exec($ch);

//close curl conn

//output content.
echo $thereturnvalue;

I've been trying to follow documentation to just get the request token so I can then move onto authentication. I'm 99% sure everything is correct but the Linkedin just gives me an error 400 meaning they're incorrect.

As well as the URL "oauth/authorize" I've also tried "oauth/requestToken" which gives the same result. (The URLs on their docs are 404's so it's hard to find out what I should be using definitively)