Search code examples
phpapioauthtumblr

first step oauth 1.0 with tumblr


ok, so i am trying to write a simple script that can authenticate with tumblr using oauth. for reasons beyond the scope of this question, i can't use any libraries, so i have to do this on my own. It seems i fail from the very first step of this. This is my code so far:

$requestTokenUrl    = "http://www.tumblr.com/oauth/request_token";
$authorizeUrl       = "http://www.tumblr.com/oauth/authorize";
$accessTokenUrl     = "http://www.tumblr.com/oauth/access_token";

$oauthCallbackUrl       = MY_URL;
$consumerKey            = MY_KEY;
$oauthTimestamp         = time();
$nonce                  = md5(mt_rand());
$oauthSignatureMethod   = "HMAC-SHA1";
$oauthVersion           = "1.0";

$sigBase = "POST&" . rawurlencode($requestTokenUrl) . "&"
 . rawurlencode(
    "oauth_callback=" . rawurlencode($oauthCallbackUrl)
    . "oauth_consumer_key=" . rawurlencode($consumerKey)
    . "&oauth_nonce=" . rawurlencode($nonce)
    . "&oauth_signature_method=" . rawurlencode($oauthSignatureMethod)
    . "&oauth_timestamp=" . $oauthTimestamp
    . "&oauth_version=" . $oauthVersion
 );

$sigKey = $consumerSecret . "&";

$oauthSig = base64_encode(hash_hmac("sha1", $sigBase, $sigKey, true));

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $requestTokenUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
 'Authorization: OAuth oauth_consumer_key="' . $consumerKey . '",
    oauth_timestamp="' . $oauthTimestamp . '",
    oauth_nonce="' . $nonce . '",
    oauth_callback="' . $oauthCallbackUrl . '",
    oauth_signature_method="' . $oauthSignatureMethod . '",
    oauth_version="' . $oauthVersion . '",
    oauth_signature="' . $oauthSig . '"'
));

curl_setopt($ch, CURLOPT_POST, 1);

$output = curl_exec($ch);

The problem is, that i wrote the above code, following a tutorial about oauth and twitter. I tried to combine some of the not so good tumblr api documentation. And now i get an oauth 400 bad request response which i cant debug somehow. Actually the oauth response is this:

HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

and the curl_getinfo shows this:

'url' => string 'http://www.tumblr.com/oauth/request_token' (length=41)
'content_type' => string 'text/html' (length=9)
'http_code' => int 400
'header_size' => int 97
'request_size' => int 545
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 0
'total_time' => float 0.377313
'namelookup_time' => float 0.028666
'connect_time' => float 0.198192
'pretransfer_time' => float 0.198388
'size_upload' => float 0
'size_download' => float 90
'speed_download' => float 238
'speed_upload' => float 0
'download_content_length' => float -1
'upload_content_length' => float -1
'starttransfer_time' => float 0.37721
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '66.6.43.30' (length=10)
'certinfo' => 
 array (size=0)
   empty
'primary_port' => int 80
'local_ip' => string '192.168.1.70' (length=12)
'local_port' => int 52189

Is there a way to know what is going wrong? Any help would be appreciated. Thank you.


Solution

  • ok found the problem. in the curl options i had

    curl_setopt($ch, CURLOPT_POST, 1);
    

    but i also needed to add:

    curl_setopt($ch, CURLOPT_POSTFIELDS, array());