Begin main content

AQXMLParser + OAuth

I've been enjoying using Jim Dovey's excellent AQXMLParser lately, and in fact am giving a talk on it tonight at Sydney CocoaHeads. To use it in a new project, I need to be able to authorise the outbound REST request with some OAuth 1 tokens etc.

OAuth can be a right pain, and the Cocoa implementations aren't awesome. gtm-oauth (the OAuth 1 library from the Google Toolbox for Mac) is a little odd in its implementation, but doing your own OAuth implementation would be so tedious it's one of the better options out there.

It's all pretty easy. Firstly, use gtm-oauth like normal to have your user authorise your app and receive a token.

Next, to make a request, gtm-oauth provides a way to authorise an NSMutableURLRequest, but AQXMLParser uses it's own HTTPMessage class (which wraps the lower level CFHTTPMessage from Core Foundation).

What I have done is extend HTTPMessage to provide a copy of itself as an NSURLRequest, and then write a category that auths that with gtm-oauth and then cribs the headers from that and applies them to the HTTPMessage. It's a little roundabout, but because of the need to sign the whole request and the way that gtm-oauth internals work, anything else would be very complicated. So in use, it looks like this:

#import "HTTPMessage+GTMOAuth.h"

...

GTMOAuthAuthentication * oauth = ...; // setup gtm-oauth like normal

HTTPMessage * myMessage = ...; // create the http request like normal

[myMessage authorizeWithGTMOAuth:oauth];

// now create the parser and use the http message etc. like normal

For now the changes and category are only in my fork, but I have a pending pull request with Jim that I will work out the best way to incorporate them into his master repository. My fork also contains tweaks to work with the latest llvm and ARC (although note neither gtm-oauth nor HTTPMessage or AQXMLParser use ARC, so you'll have to add the -fno-objc-arc compiler flag where appropriate).

gtm-outh: http://code.google.com/p/gtm-oauth/

My fork of aqtoolkit: https://github.com/aufflick/aqtoolkit

02:25 AM, 02 Aug 2012 by Mark Aufflick Permalink

Add comment