Alert Birds, OAuth, and Loggly
In today's edition of the Loggly Blog, we're going to talk about integrating with Loggly using OAuth. You can authenticate against Loggly in a couple different ways, but we're going to focus on OAuth today, because that's how Alert Birds is able to run saved searches on a user's behalf. OAuth can be a little tricky to set up at first, which is a big reason why we're open-sourced Alert Birds, and the code can be found at http://github.com/loggly/alertbirds-community-edition.
To build an app, the very first thing you'll need to do is to register an app in your Loggly instance, at /account/applications/. Apps are called 'Consumers' in OAuth parlance, and after your application gets approved, you can get started working with our API using OAuth.
In basic terms, the OAuth flow looks something like this:
1. Get a request token, which is good for a single request
2. Hit the authorization URL, and pass the request token
3. Log into Loggly when redirected
4. Authorize the application with your consumer key to access Loggly on your behalf
5. Get redirected back to your app, and with the OAuth verifier and the request token, get an access token, which is what you'll need to make future requests
This sounds pretty simple, but everything has to be just right for the flow to work. Query string parameters have to be encoded in the right order, you need to send the correct HTTP method that Loggly is expecting, and you can't lose the OAuth verifier or the request token, because if the access token request fails, you'll have to ask the user to re-authenticate your app - no good!
The OAuth wrapper you'll probably want to use is here: https://github.com/loggly/alertbirds-community-edition/blob/master/lib/oauth.py, and a working client code implementation is here: http://github.com/loggly/alertbirds-community-edition/blob/master/controllers/main.py. The wrapper class simply wraps the OAuth 2 Python library, and makes it a little easier to interact with Loggly, and the client code is where you'll find the interesting stuff in terms of hanging onto the request token, handling the return from Loggly, etc.
A simplified walkthough of what's happening in main.py is this:
That's pretty much all there is to it. Using the lib/oauth.py wrapper from Alert Birds should make it considerably easier to get going. Happy coding!
Heloise 12 Oct, 2011 09:41pm
Well put, sir, well put. I’ll cetarinly make note of that.
Servena 21 Jan, 2012 09:45pm
Life is short, and this article saved vaualble time on this Earth.