In this example, we will build a barebones AppEngine application that uses Facebook for login. The complete source code for this example can be found in examples/oauth directory located at facebook python library code.
You can see the example in action at Facebook OAuth Demo.
Before you begin this tutorial, you need to have a facebook application registered and keep its ApplicationID and Application Secret handy with you.
We will create a user model class because we need to store user’s information once he signs up with our web application. Here is an example User Model Class for AppEngine data store.
This is the most important section of the tutorial where we handle Facebook Authentication functionality. The user login flow works like this:
When user clicks on login button, we redirect user to Facebook Authorize URLwhere he is shown Facebook Login Screen and then Facebook Permission Screen. It is very easy to construct authorize url. It is Facebook OAuth URL with a few additional parameters; client_id and redirect_url. There are other parameters as well related to permissions and display style etc. but are not needed for this example. So in our case, following lines does the magic.
The first line assigns args to a python dict which contains client_id as your app_idand redirect_uri points to /login URL.
Second Step of the process is, when a user authorizes our Facebook Application and grants necessary permissions. Facebook OAuth Server redirects user to redirect_uriURL specified in previous step with an additional argument code. If you look at the code closly, both of these steps are handled by same method get of LoginHandlerclass. The presence of code parameter in the request differentiate the first step from the second. Now once we detect the user has granted access to our Application, we request for access_token like this.
Note that request_uri parameter needs to be same in both the steps of constructingauth URL as well as access_token request.
Now once we have facebook access token, we can make calls to Graph API and get required information for user and his connections etc and save in database. You can use Facebook Python library’s objects method get_object with id as “me” or call the Graph API’s URL directly.
Session Management and Logging out
Once user is authenticated and his basic profile has been created in our app, we want to identify the user in every request henceforth and personalize our application for this user. We use browser cookie to maintain session state. It involves two steps explained below:
Setting Cookie: As soon as we create or update user profile information in previous step, we set browser cookie containing facebook user-id of the user in a secured way. Facebook App Secret is used in generating cookie content’s digest and simple encoding scheme is used to encode the cookie content. This is achieved through cookie_signature and set_cookie function in the code. Following line is the top level code setting cookie.
Parsing Cookie: Every Request, we parse the cookies in the request to detect logged in user. Please refer to parse_cookie method for details of parsing cookie information. The top level code which detect if a user is present and set current_user property of the base controller.
Logging Out:* Logging out a user is pretty simple. Just delete the browser cookie and user will no longer be detected as logged-in. Following code does that.
Thats it! Hope that the article was useful. You can see the example in action at Facebook OAuth Demo.