Home » Php » api – get oauth token with tumblrs official php client

api – get oauth token with tumblrs official php client

Posted by: admin July 12, 2020 Leave a comment

Questions:

This is my first time playing with an api and oauth and tumblr has a php client. I have downloaded and installed the client with composer. This is the code they have to set up the client.

$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client->setToken($token, $tokenSecret);

I know the consumer key and secret but how do I get the token and token secret with tumblrs php client?

I also know the process of oauth but I don’t know how to actually implement it :/

How to&Answers:

Just so we’re in the same page, you can get the user’s token and secret by going through the browser sign-in flow dance. Tumblr’s flow is pretty much the same as Twitter’s so you can use this as reference: Implementing Sign in with Twitter. You can look at the OAuth part in Tumblr’s Authentication documentation to get the correct endpoints.

Note that Tumblr’s PHP client that you linked to has the default base url set to "http://api.tumblr.com/" whereas the OAuth endpoints (e.g. request_token) use "http://www.tumblr.com". To be able to use the OAuth endpoints, you will just have to change the base url. Here’s an example of the first step in the sign-in flow, getting a request token:

// Requesting for http://www.tumblr.com/oauth/request_token

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

You should get this in $result:

oauth_token=ulE1EuaZvJSN0qIKfQO5EFgcrxrOLJF0Cnm7VbLQqj66oF9nwt&oauth_token_secret=PLjC7s4JeIlgm53q7FKL1wqQkFoL0775JC6UkHKiepAQ6TxXxp&oauth_callback_confirmed=true

See this commit in Github for more info on how this was made possible.