Oauth problem sign in with twitter first step, POST to /oauth/request_token


#1

I try to implement a sign in with twitter , authentification code in PHP, I had to first make a POST request to https://api.twitter.com/oauth/request_token

That’s fail and twitter response an error 401 :

This is how I make the request:

//file 1//

<?php
require('tweeter_class.php');

$url="https://api.twitter.com/oauth/request_token";
$CallBackUrl="localhost";
$tweet=new tweeter_class();
$tweet->define_callback($CallBackUrl);
$tweet->define_base_url($url);
$tweet->define_timestamp();
$tweet->define_nonce();
$tweet->define_signature();

$auth='OAuth '.rawurlencode("oauth_consumer_key")."=".'"'.rawurlencode($tweet->oauth_consumer_key).'", '.rawurlencode("oauth_nonce")."=".'"'.rawurlencode($tweet->oauth_nonce).'", '.rawurlencode("oauth_signature")."=".'"'.rawurlencode($tweet->oauth_signature).'", '.rawurlencode("oauth_signature_method")."=".'"'.rawurlencode("HMAC-SHA1").'", '.rawurlencode("oauth_timestamp")."=".'"'.rawurlencode($tweet->oauth_timestamp).'", '.rawurlencode("oauth_version")."=".'"'.rawurlencode("1.0").'"';

$header=array('Host: api.twitter.com','User-Agent: skeleton master','accept:*/*','Authorization:'.$auth);


$c=curl_init();
curl_setopt($c,CURLOPT_URL,$url);
curl_setopt($c,CURLOPT_HTTPHEADER,$header);
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
curl_setopt($c,CURLOPT_HEADER,true);
curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($c,CURLOPT_SSL_VERIFYPEER,true);
$output=curl_exec($c);
print_r($output);
curl_close($c);

?>

//file tweet_class.php//

<?php

class tweeter_class{

var $oauth_callback;
var $oauth_consumer_key="******************************";
var $oauth_consumer_secret="*****************************";
var $oauth_nonce;
var $oauth_signature;
var $oauth_signature_method="HMAC-SHA1";
var $oauth_timestamp;
var $oauth_version="1.0";
var $HTTP_method="POST";
var $base_url;
function define_callback($val){

$this->oauth_callback=$val;

}
function define_consumer_key($val){

$this->oauth_consumer_key=$val;

}

function define_nonce(){

	$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

	$randomString = '';

    	for ($i = 0; $i<32; $i++) {

        	$randomString .= $characters[rand(0, strlen($characters) - 1)];
   		 }
  	$this->oauth_nonce=$randomString;

}


function define_timestamp(){
$date=new DateTime();
$this->oauth_timestamp=$date->getTimestamp();


}

function define_http_method($val){
$this->HTTP_method=strtoupper($val);

}
function define_base_url($val){
$this->base_url=$val;

}
function define_signature(){
$string=rawurlencode("oauth_callback")."=".rawurlencode($this->oauth_callback);
$string.="&".rawurlencode("oauth_consumer_key")."=".rawurlencode($this->oauth_consumer_key);
$string.="&".rawurlencode("oauth_nonce")."=".rawurlencode($this->oauth_nonce);
$string.="&".rawurlencode("oauth_signature_method")."=".rawurlencode("HMAC-SHA1");
$string.="&".rawurlencode("oauth_timestamp")."=".rawurlencode($this->oauth_timestamp);
$string.="&".rawurlencode("oauth_version")."=".rawurlencode("1.0");

$str=$this->HTTP_method."&".rawurlencode($this->base_url)."&".rawurlencode($string);

$str2=rawurlencode($this->oauth_consumer_secret)."&";
$key=base64_encode(hash_hmac("sha1",$str,$str2,true));
$this->oauth_signature=rawurlencode($key);
}



}

?>

If anybody sees what is wrong …thanks a lot