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
<? //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"; ?>
twitter.php: source
<? //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>"; } ?>
linkedin.php: source
<? //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>"; } ?>
Download these files from http://www.box.net/shared/oc0u7ym5y7 – Happy dancing time, in oAuth way π
good job hasin bhai …:)
thanks π
if only more people would use pecl as a base for their code instead of pear/zend framework sigh
Excellent π
You are indeed a lifesaver, now… all we need is one to work with facebook if applicable and we’re off!
Thank you π
Nice code. Thanks for posting it.
You have a small typo on line 35 in linkedin.php. laccess_ouath_token should be laccess_oauth_token; you flipped the o and a in oauth.
Thanks for noticing it, Adam π
Corrected.
I only saw because I tried it and I couldn’t figure out why it didn’t work. π
crazy typo! thanks for the debugging π
you flipped the o and a in oauth.
This is fantastic π thanks for sharing.
Paul
hey dude thnx for the nice tutorial .
am really new to this.. .
can u suggest me how i can get the pecl extension to working , so tha ti can use this code
All about banks – Clydesdale bank spl and much more.
If you disposition watching matches NFL, then you probably also proclivity to bet on the match, and predominantly on your favorite teams?
Fantastic work.
Do you have one for youtube?
I started one with PECL oAuth, but cannot get it working. I do get the key though…
function get_r_token(){
try {
$o = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
/* Google scopes are in the following format: urlencoded(scope) urlencoded(scope) */
$scopes = urlencode(“http://gdata.youtube.com/”);
$arrayResp = $o->getRequestToken(“https://www.google.com/accounts/OAuthGetRequestToken?scope={$scopes}”);
file_put_contents(OAUTH_TMP_DIR . “/request_token_resp”,serialize($arrayResp));
$authorizeUrl = “https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token={$arrayResp[“oauth_token”]}”;
if(PHP_SAPI==”cli”) {
echo “Navigate your http client to: {$authorizeUrl}\n”;
} else {
header(“Location: {$authorizeUrl}”);
}
} catch(OAuthException $E) {
echo “Response: “. $E->lastResponse . “\n”;
}
}
function get_a_token(){
try {
$o = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/request_token_resp”));
$o->setToken($request_token_info[“oauth_token”],$request_token_info[“oauth_token_secret”]);
$arrayResp = $o->getAccessToken(“https://www.google.com/accounts/OAuthGetAccessToken”);
file_put_contents(OAUTH_TMP_DIR . “/access_token_resp”,serialize($arrayResp));
echo “Finished getting the access token!\n”;
} catch(OAuthException $E) {
echo “Response: “. $E->lastResponse . “\n”;
}
}
function delete(){
$oauth = new OAuth(GOOGLE_OAUTH_CONSUMER_KEY,GOOGLE_OAUTH_CONSUMER_SECRET,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$request_token_info = unserialize(file_get_contents(OAUTH_TMP_DIR . “/access_token_resp”));
$oauth->setNonce(rand());
$oauth->setToken($request_token_info[“oauth_token”],$request_token_info[“oauth_token_secret”]);
print_r($request_token_info);
/* put the OAuth params into the Authorization header */
$oauth->setAuthType(OAUTH_AUTH_TYPE_AUTHORIZATION);
$api_args = array(“empty”=>”empty”);
$oauth->fetch(GOOGLE_YT_EDIT_VIDEO.’OJG_857fsNw’, ”, OAUTH_HTTP_METHOD_DELETE, array(“User-Agent” => “pecl/oauth”));
}
How can i make it so when it logs in the person they follow me and tweet something on their account.
Hey there- I get this error:
Fatal error: Class ‘OAuth’ not found in C:\wamp\www\site_includes\twitter.php on line 14
Little help?
Huh? I don’t understand this code at all. Where do you define the class OAuth?
$oauthc = new OAuth($oauth[‘twitter’][‘consumerkey’],
Are you using a library that you forgot to mention?
You just forgot to read the post…
Hello,
I am also getting the error like OAuth class not found in…/linkedin.php. Can you tell me what the corrections I need to do?
Also ,
I have copied all the OAuth PECL related files in the current directory…Please help!
I get this exception: exception ‘OAuthException’ with message ‘Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect)’
Which raises on this line: $data = $oauthc->fetch(‘http://api.linkedin.com/v1/people/~’);
Can anyone help me with this please? Thanks!
PS. Shouldn’t “$oauth_verifier” be used in somewhere?
I’ve fixed that problem replacing the line by this:
$data = $oauthc->fetch(βhttp://api.linkedin.com/v1/people/~β, NULL, OAUTH_HTTP_METHOD_GET);
I tried it and nothing works. All I get is this error:
PHP Fatal error: Class ‘OAuth’ not found in C:\Inetpub\wwwroot\marioninteractive.com\linkedin\linkedin.php on line 14
I checked line 14 and even the other lines and don’t see any issues.
Anyone?
Tweet me if you know the answer to my question above or just reply.
Thanks for this!! Really Great!
Hi, is this possible to be performed on a wordpress blog? If so, how exactly?
James
Finally this post help me a lot. Thanks bro for this nice post.
Some notes for people having trouble.
$oauth = new OAuth
is a standard PHP package. One that a lot of server don’t have by default. You have to install it or request your hosting company to do so (if they will). It’s not easy, but googling should help find the specifics for your OS.
Problems with getAccessToken
I found I had to change to code to the following:
$access_token_info = $oauth->getAccessToken($accesstokenurl, “”, $token);
$token is five digit number you get from linkedin.
Problems with $_REQUEST[‘oauth_token’]
This should be saved in the beginning like so:
$_SESSION[‘lrequest_oauth_token’]= $request_token_info[‘oauth_token’];
and then before setting the token, use :
$oauth_token = $_SESSION[‘lrequest_oauth_token’];
use $oauth_token instead of $_REQUEST[‘oauth_token’]
This page really has all the information I wanted about this subject and didn’t know who to ask.
Hello, Neat post. There’s an issue along with your website in web explorer, might test this? IE still is the marketplace chief and a large element of other folks will omit your great writing because of this problem.
Hey just wanted to give you a quick heads up. The words in your article seem to be running off the screen in
Firefox. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to let you know.
The layout look great though! Hope you get the issue resolved soon.
Thanks
I Am Getting this problem…Please Help Me
Class ‘OAuth’ not found in C:\xampp\htdocs\Mukesh\twitter.php on line 15
You need to install PECL oAuth extension first
Thanx Sir….