Hi Barth, I use Codeigniter as the web framework. My main code is as pasted:
<?php
class Login extends CI_Controller {
var $tokenBaseURL = 'https://api.twitter.com/oauth/request_token';
var $oauth_callback = 'http://the unencoded callback url you registered on the twitter App web site';
var $oauth_signature_method = "HMAC-SHA1";
var $oauth_version = "1.0";
var $oauth_consumer_key = 'the consumer key, without urlencode ';
var $oauth_consumer_secret = 'the consumer secret, without urlencode';
var $proxy = 'the http proxy if needed:8080';
public function Login(){
parent::__construct();
$this->load->helper('url');
$this->load->library('curl');
$this->load->library('session');
}
public function index(){
$this->load->view('login');
}
public function obtainRequestToken(){
$paramsWitoutSignature = $this->getFields2RequestToken();
$signature = $this->calculateSignature('post', $this->tokenBaseURL, $paramsWitoutSignature);
echo '
the signature is: '. $signature . '
';
$headerParams = $paramsWitoutSignature;
$headerParams['oauth_signature'] = $signature;
$headerString = $this->generateHeaderString($headerParams);
echo '
the header string is: ';
var_dump(array('Authorization' => $headerString));
echo '
';
$ch = curl_init($this->tokenBaseURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $headerString));
$output = curl_exec(($ch));
echo '
output of the requested token is: ' . $output . '
';
curl_close($ch);
}
private function generateHeaderString($params)
{
ksort($params);
$headerString = "OAuth ";
$firstPair = True;
foreach ($params as $key => $value) {
if($firstPair)
{
$firstPair = False;
}
else
{
$headerString = $headerString . ", ";
}
$headerString = $headerString . rawurlencode($key) . "=\"" . rawurlencode($value) . "\"";
}
return $headerString;
}
public function oauth2callback(){
$code = $this->input->get('code', TRUE);
$this->session->set_userdata('code', $code);
$data['code'] = $code;
$this->load->view('oauth2callback', $data);
}
private function getFields2RequestToken(){
$ts = time();
return array(
//'oauth_callback' => rawurldecode($this->oauth_callback),
'oauth_callback' => $this->oauth_callback,
'oauth_consumer_key' => $this->oauth_consumer_key,
'oauth_nonce' => trim(base64_encode($ts), '='),
//'oauth_signature' => to be calculated,
'oauth_signature_method' => $this->oauth_signature_method,
'oauth_timestamp' => $ts,
'oauth_version' => $this->oauth_version
);
}
private function calculatePostFileds($paramsArray){
$postFiliedsPairs =array();
foreach ($paramsArray as $key => $value) {
$key = rawurlencode($key);
$value = rawurlencode($value);
$postFiliedsPairs[] = "{$key}={$value}";
}
return implode('&', $postFiliedsPairs);
}
private function calculateSignature($HTTPMethod, $baseURL, $paramsArray){
//Step 1. Collecting parameters
//1. Percent encode every key and value that will be signed.
$encodedPrams = array();
foreach ($paramsArray as $key => $value) {
$encodedPrams[rawurlencode($key)] = rawurlencode($value);
}
//2. Sort the list of parameters alphabetically[1] by encoded key[2].
ksort($encodedPrams);
//3. For each key/value pair:
// Append the encoded key to the output string.
// Append the '=' character to the output string.
// Append the encoded value to the output string.
// If there are more key/value pairs remaining, append a '&' character to the output string.
$paramsString = "";
foreach ($encodedPrams as $key => $value) {
$paramsString = $paramsString . $key . "=" . $value . "&";
}
$paramsString = substr($paramsString,0,-1);
//Step 2. Creating the signature base string
//1. Convert the HTTP Method to uppercase and set the output string equal to this value.
//2. Append the '&' character to the output string.
//3. Percent encode the URL and append it to the output string.
//4. Append the '&' character to the output string.
//5. Percent encode the parameter string and append it to the output string.
$baseString = strtoupper($HTTPMethod) . '&' . rawurlencode($baseURL) . '&' . rawurlencode($paramsString);
//Step 3. Getting a signing key
//The signing key is simply the percent encoded consumer secret,
//followed by an ampersand character '&', followed by the percent encoded token secret:
$signedKey = rawurlencode($this->oauth_consumer_secret) . '&'; //. rawurlencode($this->oauth_token_secret);
//Step 4. Calculating the signature
//Finally, the signature is calculated by passing the signature base string and signing key to the HMAC-SHA1 hashing algorithm.
return base64_encode(hash_hmac('sha1', $baseString, $signedKey, true));
}
}
?>
If you suspect the calculating of your signature, I suggest you to check the result by entering your signature base string to this online testing site:http://quonos.nl/oauthTester/