One of the main reasons to adopt REST is the simplicity of the API, compared to the juggling of formats required to send and receive SOAP messages.  We’ve had a long history of creating toolkits and libraries to ease the burden of using SOAP as a transport – so much so that when I first started looking at Python again as a language (not an accident that it was around the same time Heroku added it to their polyglot platform) … I really wanted to take a step back and see how simple I could make a script functional with Force.com.  Not make something intended as fully functional application, or a web based interface, or handling multiple tasks or anything – just how simple could I make a script which logs in, performs a query, and then displays the results.  Make it as small as possible with as few dependencies as possible.

Here’s the result, a command line app which opens a browser to handle OAuth, grabs the tokens, does a SOQL query in REST and displays the results:

Twenty lines of code.  You do need to cut and paste the post login URL from the browser back into the Python script in this flow.  The only dependency is a REST library from Google. I also had to make sure my httplib was up to date (the version that comes with Snow Leopard is apparently slightly dated). The line:

Is OS X specific (opens the default web browser to the URL), but you could easily modify the script to handle a Windows based browser.  Now since writing this, I’ve gone on updated it to handle queries based on user input, and some utility functions to do things like enter and remember the token (so that you don’t have like a dozen browser tabs open…) – but even with that level of functionality the script is still more like sixty lines of code.  Next step will probably be to handle the login with “password” grant type of OAuth to remove the browser requirement completely (but I do like the browser usage as an example of how OAuth works with most use cases).

This script is among my gists on GitHub, and I’ll probably add the more functional script as a proper repository down the road.  But you can see how integration with the Force.com platform doesn’t need to be a heavy-handed affair, thanks to REST and OAuth being in your toolbox.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS