I got my OAuth working in just plain PHP, but want to make it fit within the framework.
Basically, the file below, getAccess.php, uses the class oauth.php, to run through the steps necessary to generate two tokens necessary to authenticate.
I am assuming that I would put most of the code in getaccess.php as the class. (Let's say I call it Login.class.php. That class will require_once(oauth.php).
The QForm isn't really used since now form is actually displayed on that page...?
Help, just trying to wrap my head on the best way.
The form outputs the value but really doesn't display it after I made modifications because now it just redirects and stores the returned value of the tokens in the session.
<?php
session_start();
require_once("OAuth.php");
$app_token = "YOUR APP TOKEN GOES HERE";
$app_key = "YOUR APP KEY GOES HERE";
$domain = "https://api.linkedin.com/uas/oauth";
$sig_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_consumer = new OAuthConsumer($app_token, $app_key, NULL);
$callback = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?action=getaccesstoken";
# First time through, get a request token from LinkedIn.
if (!isset($_GET['action'])) {
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "POST", $domain . "/requestToken");
$req_req->set_parameter("oauth_callback", $callback); # part of OAuth 1.0a - callback now in requestToken
$req_req->sign_request($sig_method, $test_consumer, NULL);
$ch = curl_init();
// make sure we submit this as a post
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
$req_req->to_header()
));
curl_setopt($ch, CURLOPT_URL, $domain . "/requestToken");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
//echo "</br> OUTPUT: </br>";
//echo $output;
//print_r($req_req); //<---- add this line
//print("$output\n"); //<---- add this line
parse_str($output, $oauth);
# pop these in the session for now - there's probably a more secure way of doing this! We'll need them when the callback is called.
$_SESSION['oauth_token'] = $oauth['oauth_token'];
$_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
//echo("token: " . $oauth['oauth_token'] . "</br>");
//echo("secret: " . $oauth['oauth_token_secret']);
# Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
Header('Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']);
print 'Location: ' . $domain . '/authorize?oauth_token=' . $oauth['oauth_token']; // <---- add this line
} else {
# this is called when the callback is invoked. At this stage, the user has authorised the token.
# Now use this token to get a real session token!
//print "oauth_token = [[".$_REQUEST['oauth_token']."]]\n";echo "<br/><br/>";
$req_token = new OAuthConsumer($_REQUEST['oauth_token'], $_SESSION['oauth_token_secret'], 1);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "POST", $domain . '/accessToken');
$acc_req->set_parameter("oauth_verifier", $_REQUEST['oauth_verifier']); # need the verifier too!
$acc_req->sign_request($sig_method, $test_consumer, $req_token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); //New Line
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
$acc_req->to_header()
));
curl_setopt($ch, CURLOPT_URL, $domain . "/accessToken");
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Curl error 1: ' . curl_error($ch);
}
curl_close($ch);
parse_str($output, $oauth);
$_SESSION['oauth_token'] = $oauth['oauth_token'];
$_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
echo("<BR><BR>Put These Variables in useAccess.php:");
echo("<BR>User's token: " . $oauth['oauth_token']);
echo("<BR>User's secret: " . $oauth['oauth_token_secret']);
}
.bc