Data, Maps, Usability, and Performance

Tumblr API Example using oAuth and PHP

Last updated on January 23, 2013 in Development

Tumblr API with PHP

I was recently looking at the Tumblr API and oAuth implementation in PHP. At the time, I did not have access to my server to install John Jawed’s oauth pecl package so I looked elsewhere. I did not find a lot of good examples of using oAuth with just PHP, most demos showed using username and password credentials through the old version of the Tumblr API. I wanted to see a complete oAuth example with PHP. Finally, I did find the a thread on the Tumblr API discussion group which had a really simple and well commented example of using oAuth with a modified version of Abraham’s TwitterOAuth library adapted to support the new Tumblr API. I downloaded the zip and was able to make it work, connecting to Tumblr, pressing Allow, and then making some API calls.

But, the example did not show how to make API calls without constantly needing to authenticate the application (press Allow) and I wanted to see an example of just uploading pictures to Tumblr without the extra step. When you register an application with Tumblr, you get an OAuth Consumer Key and a Secret Key for you API calls and oAuth authentication. You also specify a callback url, a place where Tumblr will send you upon successful authentication. Now, it’s important to note that some methods only require the API key (blog info, posts, likes) but any content creation requires oAuth authentication and below I will go over the basic steps of OAuth and explain how to make a post to Tumblr using oAuth, PHP, and a single API call:

The following diagram illustrates how oAuth works in general:
how oAuth works

In terms of our example below, I am going to really simplify it:

  1. End user makes a PHP request to Tumblr using OAuth Consumer Key and a Secret Key and Tumblr redirects the user to an authorization page where the user can “Allow” or “Deny” access to your application requesting permissions. (connect.php)
  2. Upon clicking “Allow” the end user is redirected to the callback page (callback.php) which responds with request token and the request token secret.
  3. At this point, the application uses the request token and the request token secret to request the oauth token and oauth token secret (callback.php).

This was already done in the connect.php and callback.php example (zip file from the thread) but the callback script never made the oauth token and oauth token secret available for later use. It would just continue to make GET Tumblr API calls. So, I have modified it to expose these values in the callback page so that you can use them at a later time for your API calls. It makes sense to save them to a database. I have included an api.php script where you can fill in your consumer_key (OAuth Consumer Key), consumer_secret (Secret Key), oauth_token, oauth_token_secret, and your tumblr blog hostname in order to create a Tumblr post through the API.

Here is a zip with all the scripts that allow you to use oauth authentication to communicate with Tumblr via API in PHP. It should be easy to modify this for Twitter API or other APIs that use oauth with PHP.


Tumblr JavaScript Client – JavaScript client for the Tumblr API (with full support for all the Tumblr API V2 endpoints)
Opauth – Multi-provider authentication framework for PHP
tumblr.js – JavaScript client for the Tumblr API
tumblr.php – Tumblr API v2 PHP Client

Tags: , ,

Facebook Twitter Hacker News Reddit More...