Ok, I know there are a lot of OAuth haters out there and maybe it is not the easiest protocol to work with but really, I was able to get connected and running in about 15 minutes. (and that included 5 minutes of Google time)
I am playing around with Klout’s API and wanted to pull in some info from twitter to augment the Klout info I was seeing. Klout is easy – as just about every Mashery API I’ve worked with has been – you just register for a key and they make your calls. Twitter however, requires OAuth. I knew the Zend_Service_Twitter had recently undergone a revamp to support OAuth but there was one small problem. The script I am playing with is all backend. There is no front end at all. It just collects info and stores it in a database so I can query it later. (I’m an old-school database guy and love just writing ad-hoc queries to see what I can see) Twitter wants to redirect you to a site once you have authorized access. Since I don’t actually have a site to redirect it to, this was a problem.
The Solution – in two parts.
Ok, second things first, this script is straight procedural code at the moment. It isn’t even good enough to qualify as prototype code, more proof-of-concept. There’s no way in heck I’m sharing it with anyone so don’t even ask. I know, however that if things progress as I expect them to, the script will end up as a Zend Framework application. For that reason I wanted to use Zend_Service_Twitter. A quick Google showed me that my good friend and Zend Framework community member Michelangelo van Dam had a tutorial already written on this very subject titled Single User Zend_Service_Twitter. Part 2 is solved because everything I need to know is in that tutorial. So I move on to the first problem, how to get a valid OAuth token and secret. It turns out, that wasn’t hard at all either, it just required a little imagination.
Jaisen Mathai wrote up a blog post titled Twitter, PHP and OAuth a little while back explaining OAuth. In it he links to a quick piece of sample code that he put together to showcase his Twitter API wrapper. The wrapper itself is good and if I had not already decided to go with Zend Framework, I would have used it. The sample code however, allowed me to authenticate with twitter and then store the token and secret, the two things you must have to make this work. So I took his sample code, put it on my development server, followed his instructions to the letter and voilà, I had my token and and token secret.
I then went back to Dragonbe’s blog post, grabbed his code, modified it slightly so that I didn’t have to use Zend_Config and it worked too!
If you want to get started with Twitter’s OAuth, the two tutorials I’ve linked to are great jumping off points. Enjoy!
Until next time,
I <3 |<