After searching for help to connect with LinkedIn via their oAuth protocol using PECL oAuth extension, I’ve found that lots of people are posting in their forum for the code samples. And only very few obscure code examples are available. I’ve found phplinkedin script but that is just too bulky for a simple oAuth dance 🙂
So here are two files to perform 3 step oAuth Dance for both twitter and linkedin. Just set your consumer key and consumer secret key in these scripts (config.php) and in your LinkedIn and Twitter application, set the url of these scripts as authentication callback 🙂 thats it 🙂
check out the source code below or just straight download them from the following url
http://www.box.net/shared/oc0u7ym5y7
config.php: source
[sourcecode lang=”php”]
<?
//config.php
$oauth[‘twitter’][‘consumersecret’]="UtNkcJC5VqmHgSgxMIRl2UcHaJLWINzr1g2q*****";
$oauth[‘twitter’][‘consumerkey’]="LveyUCUf9Ym96AU7*****";
$oauth[‘twitter’][‘requesttokenurl’]="http://twitter.com/oauth/request_token";
$oauth[‘twitter’][‘accesstokenurl’]="http://twitter.com/oauth/access_token";
$oauth[‘twitter’][‘authurl’]="http://twitter.com/oauth/authorize";
$oauth[‘linkedin’][‘consumersecret’]="SX9FS_Ptz7yNA3WtTW0e8z3_XSiROnVSpOEbAVCfKAn7fqFq4kjelVXiNMO*****";
$oauth[‘linkedin’][‘consumerkey’]="qQkxCNYQbuALhWyBZO03V–6dtwUnQHz7KFE4PBpdIL6hy_87SHygEZAJj9*****";
$oauth[‘linkedin’][‘requesttokenurl’]="https://api.linkedin.com/uas/oauth/requestToken";
$oauth[‘linkedin’][‘accesstokenurl’]="https://api.linkedin.com/uas/oauth/accessToken";
$oauth[‘linkedin’][‘authurl’]="https://api.linkedin.com/uas/oauth/authorize";
?>
[/sourcecode]
twitter.php: source
[sourcecode lang=”php”]
<?
//twitter.php
/**
* twitter authentication script based on
* pecl oauth extension
*/
session_start();
include_once("config.php");
/*
unset($_SESSION[‘trequest_token_secret’]);
unset($_SESSION[‘taccess_oauth_token’]);
unset($_SESSION[‘taccess_oauth_token_secret’]);
*/
$oauthc = new OAuth($oauth[‘twitter’][‘consumerkey’],
$oauth[‘twitter’][‘consumersecret’],
OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI); //initiate
if(empty($_SESSION[‘trequest_token_secret’])) {
//get the request token and store it
$request_token_info = $oauthc->getRequestToken($oauth[‘twitter’][‘requesttokenurl’]); //get request token
$_SESSION[‘trequest_token_secret’] = $request_token_info[‘oauth_token_secret’];
header("Location: {$oauth[‘twitter’][‘authurl’]}?oauth_token=".$request_token_info[‘oauth_token’]);//forward user to authorize url
}
else if(empty($_SESSION[‘taccess_oauth_token’])) {
//get the access token – dont forget to save it
$request_token_secret = $_SESSION[‘trequest_token_secret’];
$oauthc->setToken($_REQUEST[‘oauth_token’],$request_token_secret);//user allowed the app, so u
$access_token_info = $oauthc->getAccessToken($oauth[‘twitter’][‘accesstokenurl’]);
$_SESSION[‘taccess_oauth_token’]= $access_token_info[‘oauth_token’];
$_SESSION[‘taccess_oauth_token_secret’]= $access_token_info[‘oauth_token_secret’];
}
if(isset($_SESSION[‘taccess_oauth_token’])) {
//now fetch current users profile
$access_token = $_SESSION[‘taccess_oauth_token’];
$access_token_secret =$_SESSION[‘taccess_oauth_token_secret’];
$oauthc->setToken($access_token,$access_token_secret);
$data = $oauthc->fetch(‘http://twitter.com/account/verify_credentials.json’);
$response_info = $oauthc->getLastResponse();
echo "<pre>";
print_r(json_decode($response_info));
echo "</pre>";
}
?>
[/sourcecode]
linkedin.php: source
[sourcecode lang=”php”]
<?
//linkedin.php
/**
* linkedin authentication script based on
* pecl oauth extension
*/
session_start();
include_once("config.php");
/*
unset($_SESSION[‘lrequest_token_secret’]);
unset($_SESSION[‘laccess_oauth_token’]);
unset($_SESSION[‘laccess_oauth_token_secret’]);
*/
$oauthc = new OAuth($oauth[‘linkedin’][‘consumerkey’],
$oauth[‘linkedin’][‘consumersecret’],
OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION); //initiate
$oauthc->setNonce(rand());
if(empty($_SESSION[‘lrequest_token_secret’])) {
//get the request token and store it
$request_token_info = $oauthc->getRequestToken($oauth[‘linkedin’][‘requesttokenurl’]); //get request token
$_SESSION[‘lrequest_token_secret’] = $request_token_info[‘oauth_token_secret’];
header("Location: {$oauth[‘linkedin’][‘authurl’]}?oauth_token=".$request_token_info[‘oauth_token’]);//forward user to authorize url
}
else if(empty($_SESSION[‘laccess_oauth_token’])) {
//get the access token – dont forget to save it
$request_token_secret = $_SESSION[‘lrequest_token_secret’];
$oauthc->setToken($_REQUEST[‘oauth_token’],$request_token_secret);//user allowed the app, so u
$access_token_info = $oauthc->getAccessToken($oauth[‘linkedin’][‘accesstokenurl’]);
$_SESSION[‘laccess_oauth_token’]= $access_token_info[‘oauth_token’];
$_SESSION[‘laccess_oauth_token_secret’]= $access_token_info[‘oauth_token_secret’];
$_SESSION[‘loauth_verifier’] = $_REQUEST[‘oauth_verifier’];
}
if(isset($_SESSION[‘laccess_oauth_token’])) {
//now fetch current user’s profile
echo "<pre>";
$access_token = $_SESSION[‘laccess_oauth_token’];
$access_token_secret =$_SESSION[‘laccess_oauth_token_secret’];
$oauth_verifier = $_SESSION[‘loauth_verifier’];
$oauthc->setToken($access_token,$access_token_secret);
$data = $oauthc->fetch(‘http://api.linkedin.com/v1/people/~’);
$response_info = $oauthc->getLastResponse();
print_r(htmlspecialchars($response_info));
echo "</pre>";
}
?>
[/sourcecode]
Download these files from http://www.box.net/shared/oc0u7ym5y7 – Happy dancing time, in oAuth way 😉