Search code examples
phpoauthtumblr

How do I get the access_token from tumblr's official php client?


I've followed the directions posted in this stackoverflow question, but I am stuck.

I am using tumblr/tumblr.php from Github (the official "PHP client for the tumblr API").

I am also following directions here (which are actually for twitter), but those directions aren't tailored for the git library I am using.

I have a valid consumer key and secret.

From those I make a request and get oauth_token and oauth_token_secret like so:

$client = new Tumblr\API\Client($consumerKey,$consumerSecret);
$client->getRequestHandler()->setBaseUrl('https://www.tumblr.com/');
$req = $client->getRequestHandler()->request('POST', 'oauth/request_token', [
  'oauth_callback' => '...',
]);
// Get the result
$result = $req->body->__toString();
print_r( $result );

Which gives me:

oauth_token=2C6f...MqSF&oauth_token_secret=HaGh...IJLi&oauth_callback_confirmed=true

Then I send the user to the http://www.tumblr.com/oauth/authorize?oauth_token=2C6f...MqSF, so they can allow access for the app. This redirects to: ...?oauth_token=2C6f...MqSF&oauth_verifier=nvjl...GtEa#_=_

And now in the final step I believe I am supposed to convert my request token to an access token. Is that right? I am doing something wrong:

$client = new Tumblr\API\Client($consumerKey,$consumerSecret);
$client->getRequestHandler()->setBaseUrl('https://www.tumblr.com/');
$req = $client->getRequestHandler()->request('POST', 'oauth/access_token', [
  'oauth_token' => '2C6f...MqSF',
  'oauth_verifier' => 'nvjl...GtEa'
]);
// Get the result
$result = $req->body->__toString();
print_r( $result );

because I get responses like this one:

oauth_signature [AqbbYs0XSZ7plqB0V3UQ6O6SCVI=] does not match expected value [0XwhYMWswlRWgcr6WeA7/RrwrhA=]

What is wrong with my last step?

I am not sure if I should even be sending oauth_verifier with the request. Is #_=_ supposed to be part of oauth_verifier? I wouldn't think so. I get signature errors for all the variations Ive tried.

Without the token and tokenSecret I can't make certain calls to the API. I get unauthorized 403 responses. Same when I use the token and token_secret from the second step. I'm pretty sure I need a new token/secret pair.


Solution

  • You're pretty close, you just are passing the oauth_token incorrectly in the last step, and skipping out on oauth_token_secret altogeter.

    I've compiled this working code (which you can also now find posted on the Wiki at https://github.com/tumblr/tumblr.php/wiki/Authentication):

    <?php
    
    require_once('vendor/autoload.php');
    
    // some variables that will be pretttty useful
    $consumerKey = '<your consumer key>';
    $consumerSecret = 'your consumer secret>';
    $client = new Tumblr\API\Client($consumerKey, $consumerSecret);
    $requestHandler = $client->getRequestHandler();
    $requestHandler->setBaseUrl('https://www.tumblr.com/');
    
    // start the old gal up
    $resp = $requestHandler->request('POST', 'oauth/request_token', array());
    
    // get the oauth_token
    $out = $result = $resp->body;
    $data = array();
    parse_str($out, $data);
    
    // tell the user where to go
    echo 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
    $client->setToken($data['oauth_token'], $data['oauth_token_secret']);
    
    // get the verifier
    echo "\noauth_verifier: ";
    $handle = fopen('php://stdin', 'r');
    $line = fgets($handle);
    
    // exchange the verifier for the keys
    $verifier = trim($line);
    $resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
    $out = $result = $resp->body;
    $data = array();
    parse_str($out, $data);
    
    // and print out our new keys
    $token = $data['oauth_token'];
    $secret = $data['oauth_token_secret'];
    echo "\ntoken: " . $token . "\nsecret: " . $secret;
    
    // and prove we're in the money
    $client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
    $info = $client->getUserInfo();
    echo "\ncongrats " . $info->user->name . "!\n";