This example shows how to create a secure, private RESTful API using Django, Tastypie, and the xAuth variant of OAuth 1.0a, which can be deployed to Heroku.
The API has two resources, Users
and Tasks
which are like simple TODO items.
I wanted to create a simple REST backend for a mobile client. I also wanted this API to be private, meaning that only clients I designate could access it. Finally, I wanted it to be easy to deploy or "Heroku-compatible".
I did a lot of research, and came to the conclusion that OAuth was the way to go for authentication and authorization. However, the full three-legged OAuth workflow seemed needlessly overcompliated. Then I recalled Twitter's xAuth*, which allows client to obtain an access token and secret with a user name and password. This seemed like a much better fit for a mobile app.
* xAuth is also used by Instapaper's Full API.
- Tastypie (and Django) -- I feel like Tastypie gets REST right, and I like Django so this was an easy starting point. Also, Tastypie supports OAuth via the django-oauth-plus app (but not xAuth!).
- xAuth via my fork of django-oauth-plus -- django-oauth-plus does not support xAuth, so I added support for it myself. Some inspiration was drawn from django-piston-xauth.
- Heroku -- Heroku is the easiest way to deploy a Django app that I've found. It has some drawbacks, but it can't be beat for getting something up and running quickly.
This app is already running at http://django-api-example.herokuapp.com. It includes an example user with read-only access. A few simple steps to query the API follow.
-
Install
oauth-proxy
.oauth-proxy
makes is easy to use OAuth withcurl
. -
Run the
example/client.py script
. It should generate an OAuth access token and secret:$ python client.py
{'oauth_token_secret': 'WBGYYUjasZ2LFsQF', 'oauth_token': 'dac6e92dc42e42e685e39728cf6e523f'}
-
Start
oauth-proxy
with the oauth_token and oauth_token_secret from the output ofclient.py
:$ oauth-proxy --consumer-key=testconsumer --consumer-secret=secret --token=dac6e92dc42e42e685e39728cf6e523f --token-secret=WBGYYUjasZ2LFsQF
-
Get some data via
curl
:$ curl -i -x localhost:8001 http://django-api-example.herokuapp.com/api/v1/tasks/
-
Create a virtualenv and install requirments
$ mkvirtualenv django-api-example
$ pip install -r requirements.txt
-
Create a new app and add the remote.
$ heroku apps:create <app name> --stack=cedar
$ git remote add heroku git@heroku.com:<app name>.git
-
Add a
DEPLOY_CONFIG
variable to distinguish between local and production environments.$ heroku config:add DEPLOY_CONFIG=production
-
Push
$ git push heroku master
-
Set up the database.
$ heroku run django_api_example/manage.py syncdb
-
Set up initial data with the included test fixture. This creates two users,
admin
andtest
. The passwords are the same as the username. This is obviously absurdly insecure should be changed in production. The test fixture also creates a consumer with keyconsumer
and secretsecret
. Again these should be changed. Finally it creates anall
resource (a django-oauth-plus requirement) and sets up aBasic
group that has read/write priveledges toTasks
.$ heroku run django_api_example/manage.py loaddata django_api_example/fixtures/test_data.json
-
Edit
example/client.py script
, changingconsumer_key
,consumer_secret
andaccess_token_url
to the appropriate values. Runexample/client.py
to generate an access token and secret. -
Start
oauth-proxy
with your consumer and access token information$ oauth-proxy --consumer-key=consumer --consumer-secret=secret --token=<access token> --token-secret=<access token secret>
-
Post some data
curl -i -x localhost:8001 -X POST --data '{"text":"buy milk"}' -H "Content-Type: application/json" http://your-app.herokuapp.com/api/v1/tasks/
-
Verify it was created
curl -i -x localhost:8001 http://django-api-example1.herokuapp.com/api/v1/tasks/
The MIT License
Copyright (c) 2012 Andy Mroczkowski
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.