Developer Centre
Python Walkthrough
|
This page will get you started writing your first Fire Eagle application using Python. While this example will help you build a command-line Python application, many of the ideas and code will also apply if you're building a web-based application using Python (Django, for instance.) We will try to point out places where a web-based application will differ significantly from the command-line application described below. Table of ContentsSetupBackgroundFire Eagle uses OAuth to identify and authenticate applications and users that interact with the system. If you don't know anything about OAuth, read more about it here. Most of this walkthrough will focus on getting your application to authenticate a user using OAuth. Once that's done your application will be able to access Fire Eagle's API. Python Stuff
Of course you'll need a Python interpreter. You'll also need to download the OAuth Python Library. The
Put the oauth.py file some place that your Python script will be able to find it. Create a directory called Fire Eagle StuffBefore we get started you need to tell Fire Eagle a little something about your application to get a consumer key and consumer secret which will be used to identify your application to Fire Eagle. To create a new app click here (or click the "Create new application" button from the "Developer" home page). Most of the fields you need to fill in should be fairly self-explanatory. The one interesting bit is "Application Type". For this example we'll choose "Desktop". If your code will someday run on a web server, you'll have to change your application type to "Web". Read more about application types here. When you submit the new application form, you'll get a consumer key and consumer secret -- jot those down. You can always check them again in the Manage Applications section. Enough setup. Let's write some code! Let's Write Code!You can follow along as we build this application, or you can download the complete finished example app and figure out what's going on for yourself. (The downloadable version contains a few tricks not covered in the example, but should be similar enough...) Imports and ConstantsFirst we need to do a little code setup. Start off your 'my_app.py' file with the following lines, which basically just set up a bunch of constants we'll use later:
Script Structure
We'll add in a little structure -- an entry point which will call our main procedure,
The only thing that's actually interesting here is the
Go ahead and add the following code to
OAuth StuffThere are a few steps involved getting an OAuth access token for each of your users. Read about user authorization in Fire Eagle. For full details, read the OAuth spec . Once you've got an access token, you can store it (associated with your user) and reuse the same token until the user revokes the token (tells Fire Eagle that they don't want to share location with your app any more.) Storing the access token will be one of the bigger changes from the example as presented to an app that runs on a web server. The example assumes that it runs on a single user machine so can use a single file location to store the token. On a web server you'd need to associate each access token with a particular user. You could store the token in a cookie, but it would probably be much safer/smarter to store tokens in a database linked to usernames or user IDs. But overall the concepts (and much of the code) from the example should translate reasonably well to the web... Get a Request TokenThis step is pretty easy, since the OAuth library does almost everything for you. We create a new OAuthRequest (telling it our consumer key and consumer secret and the URL we'll be making the request from and a callback URL - use 'oob' if you don't support callbacks.)
OAuthRequest knows how to generate a signature for this request and has a
Our The most likely reason for an unexpected response is something wrong with your consumer key or consumer secret -- double check the Fire Eagle site to make sure you copied them correctly into your code. Of course examining the actual server response should help you figure out what's going on here.
Add the following lines right after the setup code in
You should now be able to run this script and get a request token from Fire Eagle. Not too thrilling, but a start... Have your user(s) authorize your appAgain the code in this step is pretty simple. The main difference to the previous step is that the URL isn't signed with OAuth. We will generate a URL encoding the request token we obtained in the previous step (note the token parameter when we create the new OAuthRequest). The user (in the example case, the user is you) needs to go to this URL to tell Fire Eagle that it's OK for your sample app to access your location. The script will print a URL that you cut and paste into a web browser linking to a form for you to fill out. When you're done, you'll tell the script that it's OK to continue.
This step would be a little different for a web application. You could directly redirect your users to the If your app does not support callbacks, Fire Eagle has no way to contact your app after the user authorizes it, so the script just waits for the user to say they're done.
Once the user has authorized your app, Fire Eagle provides the user with an Add the following code after the request token code from the previous step:
You can run the script again now to make sure everything still works, but it still doesn't do much... Exchange the Request Token for an Access TokenNow that the user has authorized our request token, we can exchange it for an access token (and an access token is really what we want since that's what we'll need to access the API). This step is almost the same as the request token step, except that we pass our current request token as a parameter when creating the new OAuthRequest. You have to provide the oauth_verifier obtained in the previous step to FireEagle, when asking for access token. There are also a few more opportunities for things to go wrong -- the most likely being that the request token was not actually approved before we executed this step... Add the following after the authorize token code:
Feel free to test the script again, but it's not quite done yet. Using the Fire Eagle APINow we're finally able to do something useful. The script will ask the user for a location (try entering an address or city name or something like that) and attempt to update their Fire Eagle location to that address. We need to tell the OAuthRequest about an additional address parameter so that the address can be included in the signature. But the pattern is the same: create a new OAuthRequest, pass in consumer info, an access token, the URL we want to access, and some additional parameters.
There is one more subtle point here -- Fire Eagle requires us to submit location updates as HTTP POSTs (rather than GETs like we've been using for everything else.) So instead of calling
If we wanted to do a query instead of an update we'd do a GET instead of POST and
For now we won't worry about parsing Fire Eagle's response and we'll just assume that the update worked. The response will indicate an error code and message if it has failed. A success response merely acknowledges that the request was received, not that it was handled correctly. Go check your location on the Fire Eagle website to see if the update worked. Finishing UpSo you've probably realized by now that it's quite a hassle to go through the process of authorizing a request token every time you run the script. It'd be much more convenient to store an access token once we get one. Check out the full version of the example code for a version that writes access tokens to file. |