401 Unauthorized : Failed to validate oauth signature and token


#1

Using this code within PHP I get the following error.

HTTP/1.1 100 Continue HTTP/1.1 401 Unauthorized Date: Sat, 01 Oct 2011 09:55:53 GMT Status: 401 Unauthorized X-Transaction: d77d0f11137f00dd X-Frame-Options: SAMEORIGIN Last-Modified: Sat, 01 Oct 2011 09:55:53 GMT X-Runtime: 0.00828 Content-Type: text/html; charset=utf-8 Content-Length: 44 Pragma: no-cache X-Content-Type-Options: nosniff X-Revision: DEV Expires: Tue, 31 Mar 1981 05:00:00 GMT Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 X-MID: cb5362513fa092c7e2aac8c800cc7d021dd130ce Set-Cookie: k=10.35.36.120.1317462953910162; path=/; expires=Sat, 08-Oct-11 09:55:53 GMT; domain=.twitter.com Set-Cookie: guest_id=v1%3A131746295391962890; domain=.twitter.com; path=/; expires=Mon, 30 Sep 2013 21:55:53 GMT Set-Cookie: _twitter_sess=BAh7CDoPY3JlYXRlZF9hdGwrCMBP6b4yAToHaWQiJTdlNGY5OTY4Y2Y5NjA3%250AYWVhMTg3NGRkMzM4MTIxNjJlIgpmbGFzaElDOidBY3Rpb25Db250cm9sbGVy%250AOjpGbGFzaDo6Rmxhc2hIYXNoewAGOgpAdXNlZHsA–efd3b7e43881e72a9e475b365d7ef39e4f6c8a9b; domain=.twitter.com; path=/; HttpOnly Vary: Accept-Encoding Server: tfe Failed to validate oauth signature and token

<?php class Twitter{ public static function getConsumerKey(){return '######';} public static function getConsumerSecret(){return '######';} static public function GenerateOauthNonce(){ $mt = microtime(); $rand = mt_rand(); return md5($mt . $rand); } static public function Authorize(){ $uri = 'http://api.twitter.com/oauth/request_token'; $params = Array(); $params['oauth_consumer_key'] = Twitter::getConsumerKey(); $params['oauth_nonce'] = Twitter::GenerateOauthNonce(); $params['oauth_signature_method'] = 'HMAC-SHA1'; $params['oauth_timestamp'] = time(); $params['oauth_version'] = "1.0"; foreach ($params as $k => $v){ $params[$k] = urldecode($v); } $uri = urldecode($uri); $BaseString = ''; foreach ($params as $k => $v){ $BaseString = $BaseString . '&' . $k . '=' . $v; } $BaseString = 'POST&' . $uri . urldecode($BaseString); echo $BaseString; $params['oauth_signature'] = base64_encode(hash_hmac('SHA1', $BaseString, Twitter::getConsumerSecret()."&", TRUE)); echo ''; foreach ($params as $k => $v){ echo $k . '=>' .$v . ''; } echo ''; $session = curl_init($uri); curl_setopt ($session, CURLOPT_POST, true); curl_setopt ($session, CURLOPT_POSTFIELDS, $params); curl_setopt($session, CURLOPT_HEADER, true); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($session); curl_close($session); return $response; } } ?>