Register with Oauth – Microsoft (Live)

For this last post in the series on oauth we’ll take a look at Microsoft (aka Hotmail, aka Live). But before we begin, read the overview / disclaimer. Now for Microsft …

First off, ensure that you have registered your application with Microsoft and have created the following in your settings file:

1) Create the redirect URL

The first step in the oauth handshake is to redirect to Microsoft with some application specific data in the URL including your client ID, scope (what data you want access to) and redirect URL (where you want Microsoft to send the user after they’ve approved / denied the application)

2) Redirect to provider’s site

This url can now be used to redirect to Microsoft (i.e. HttpResponseRedirect(url)):

3) Handle the response (approved or denied)

Once the user makes their choice to approve or deny, Microsoft will redirect back to your redirect_url. You will need to verify the user approved the application:

If any of the above tests fail, we can safely assume the user either arrived at this page directly (i.e. by typing in the URL) or they denied the application. Either way we don’t want to proceed and should redirect them to the start of the registration flow (i.e. HttpResponseRedirect(reverse('register'))).

4 & 5) Get an access token and the user’s profile

At this point the user has authorized your application but you don’t have actual access to their data yet. To get that you’ll need to request an access token. Notice that we’re saving the access token to the user’s session as we don’t want to request it more than once during the registration flow.

Once you have the access token, you can then make the request for the user’s profile data:

That’s it! You can now use the user’s profile information to pre-fill a registration form, perhaps skipping over fields where you already have a required value such as an email address or first and last name. Just be sure to save their Microsoft ID along with their profile so you can use it validate them in the future. You should also save the access_token and expires values so that you can make future requests to the API for this user or refresh the access token when it has expired.

In a future post I’ll be looking at how to detect an already registered user as well as provide a login with this provider button.

Django Register with Oauth – Yahoo!

Continuing the oauth series but before we begin, read the overview / disclaimer. On to Yahoo!…

For this tutorial you will need Oauth2 installed.

First off, ensure that you have registered your application with Yahoo! and have created the following in your settings file:

1) Create the redirect URL

Yahoo is a little bit more complicated than the previous providers in that the first step requires you to get a request token before redirecting to Yahoo!. Much like the other examples, this also requires a redirect URL and the consumer key, but we also have some new data that’s required including a timestamp, nonce (any unique string), version and language.

Once you’ve got the request token, you can use it to create the authorization URL.

2) Redirect to provider’s site

This url can now be used to redirect to Yahoo! (i.e. HttpResponseRedirect(url)):

3) Handle the response (approved or denied)

Once the user makes their choice to approve or deny, Yahoo! will redirect back to your redirect_url. You will need to verify the user approved the application:

If any of the above tests fail, we can safely assume the user either arrived at this page directly (i.e. by typing in the URL) or they denied the application. Either way we don’t want to proceed and should redirect them to the start of the registration flow (i.e. HttpResponseRedirect(reverse('register'))).

4 & 5) Get an access token and the user’s profile

At this point the user has authorized your application but you don’t have actual access to their data yet. To get that you’ll need to request an access token. Notice that we’re saving the access token to the user’s session as we don’t want to request it more than once during the registration flow.

Once you have the access token, you can then make the request for the user’s profile data:

That’s it! You can now use the user’s profile information to pre-fill a registration form, perhaps skipping over fields where you already have a required value such as an email address or first and last name. Just be sure to save their Yahoo! ID along with their profile so you can use it validate them in the future. You should also save the access_token and expires values so that you can make future requests to the API for this user or refresh the access token when it has expired.

In a future post I’ll be looking at how to detect an already registered user as well as provide a login with this provider button.

Django Register with Oauth – Google

Before we begin, read the overview / disclaimer. Now for Google…

First off, ensure that you have registered your application with Google and have created the following in your settings file:

1) Create the redirect URL

The first step in the oauth handshake is to redirect to Google with some application specific data in the URL including your client ID, scope (what data you want access to) and redirect URL (where you want Google to send the user after they’ve approved / denied the application)

2) Redirect to provider’s site

This url can now be used to redirect to Google (i.e. HttpResponseRedirect(url)):

3) Handle the response (approved or denied)

Once the user makes their choice to approve or deny, Google will redirect back to your redirect_url. You will need to verify the user approved the application:

If any of the above tests fail, we can safely assume the user either arrived at this page directly (i.e. by typing in the URL) or they denied the application. Either way we don’t want to proceed and should redirect them to the start of the registration flow (i.e. HttpResponseRedirect(reverse('register'))).

4 & 5) Get an access token and the user’s profile

At this point the user has authorized your application but you don’t have actual access to their data yet. To get that you’ll need to request an access token. Notice that we’re saving the access token to the user’s session as we don’t want to request it more than once during the registration flow.

Once you have the access token, you can then make the request for the user’s profile data:

That’s it! You can now use the user’s profile information to pre-fill a registration form, perhaps skipping over fields where you already have a required value such as an email address or first and last name. Just be sure to save their Google ID along with their profile so you can use it validate them in the future. You should also save the access_token and expires values so that you can make future requests to the API for this user or refresh the access token when it has expired.

In a future post I’ll be looking at how to detect an already registered user as well as provide a login with this provider button.

Django Register with Oauth – Twitter

Before we begin, read the overview / disclaimer. Now for Twitter…

For this tutorial you’ll need to install Tweepy.

First off, ensure that you have registered your application with Twitter and have created the following in your settings file:

1) Create the redirect URL

The first step in the oauth handshake is to redirect to Twitter with the oauth token (generated when the request token is fetched below):

2) Redirect to provider’s site

This url can now be used to redirect to Twitter (i.e. HttpResponseRedirect(url)):

3) Handle the response (approved or denied)

Once the user makes their choice to approve or deny, Twitter will redirect back to your redirect_url. You will need to verify the user approved the application:

If any of the above tests fail, we can safely assume the user either arrived at this page directly (i.e. by typing in the URL) or they denied the application. Either way we don’t want to proceed and should redirect them to the start of the registration flow (i.e. HttpResponseRedirect(reverse('register'))).

4 & 5) Get an access token and the user’s profile

At this point the user has authorized your application but you don’t have actual access to their data yet. To get that you’ll need to request an access token. Notice that we’re saving the access token to the user’s session as we don’t want to request it more than once during the registration flow.

Once you have the access token, you can then make the request for the user’s profile data:

One thing to notice is that a custom parser is being used to get the raw json data coming from Twitter. Here’s what that parser looks like:

That’s it! You can now use the user’s profile information to pre-fill a registration form, perhaps skipping over fields where you already have a required value such as an email address or first and last name. Just be sure to save their Twitter ID along with their profile so you can use it validate them in the future. You should also save the access token key and secret so that you can make future requests to the API for this user. Note that Twitter does not currently expire access tokens like other providers.

In a future post I’ll be looking at how to detect an already registered user as well as provide a login with this provider button.

Django Register with Oauth – Facebook

Now that you’ve read the overview / disclaimer, let’s dive into Facebook.

First off, ensure that you have registered your application with Facebook and have created the following in your settings file:

1) Create the redirect URL

The first step in the oauth handshake is to redirect to Facebook with some application specific data in the URL including your application ID and redirect URL (where you want Facebook to send the user after they’ve approved / denied the application):

2) Redirect to provider’s site

This url can now be used to redirect to Facebook (i.e. HttpResponseRedirect(url)):

3) Handle the response (approved or denied)

Once the user makes their choice to approve or deny, Facebook will redirect back to your redirect_url. You will need to verify the user approved the application:

If any of the above tests fail, we can safely assume the user either arrived at this page directly (i.e. by typing in the URL) or they denied the application. Either way we don’t want to proceed and should redirect them to the start of the registration flow (i.e. HttpResponseRedirect(reverse('register'))).

4 & 5) Get an access token and the user’s profile

At this point the user has authorized your application but you don’t have actual access to their data yet. To get that you’ll need to request an access token. Notice that we’re saving the access token to the user’s session as we don’t want to request it more than once during the registration flow.

Once you have the access token, you can then make the request for the user’s profile data:

That’s it! You can now use the user’s profile information to pre-fill a registration form, perhaps skipping over fields where you already have a required value such as an email address or first and last name. Just be sure to save their Facebook ID along with their profile so you can use it validate them in the future. You should also save the access_token and expires values so that you can make future requests to the API for this user or refresh the access token when it has expired.

In a future post I’ll be looking at how to detect an already registered user as well as provide a login with this provider button.

Django Register with Oauth – Disclaimer and Overview

UPDATE… links to all articles…

Facebook – https://chriskief.com/2012/12/22/django-register-with-oauth-facebook/
Twitter – https://chriskief.com/2012/12/22/django-register-with-oauth-twitter/
Google – https://chriskief.com/2012/12/22/django-register-with-oauth-google/
Yahoo! – https://chriskief.com/2012/12/30/django-register-with-oauth-yahoo/
Microsoft – https://chriskief.com/2013/01/05/register-with-oauth-microsoft-live/


First, a couple notes before we begin…

These examples are for server-side implementations, not client-side JavaScript. I’m also going to assume you have some Python / Django experience and will leave it up to you as to how you ultimately structure your code. I’ll also warn that there are probably better ways to do some of this (this is only week 2 of Python for me), but these get the job done so they’ll do for now. I reserve the right to ridicule my own code at some future date.


The flow for each provider is basically the same:

  • Create the URL to redirect to the provider (sometimes this includes fetching a request token)
  • Redirect the user to the provider’s site for approval
  • Handle the response (approved or denied)
  • If the user approved, get an access token
  • Get the user’s profile

Let’s look at Facebook first…

Django Register with Oauth – Facebook, Twitter, Google, Yahoo! and Hotmail (Live)

At first this seems simple enough, allow users to register for your site using one of their existing accounts at popular sites like Facebook, Twitter, Google, Yahoo! and Hotmail (Live). Then you realize that each one has a slightly different oauth implementation, the documentation is not that great, their SDK is out-of-date (looking at you Yahoo), various tutorials on the web are incomplete / out-of-date / simply wrong, or the existing packages provide more functionality than you need (i.e. you’ve already got a user management framework in place).

Over the next couple of posts I will look at getting each one of these up and running. But first, you’ll need to register your application with each provider:

Facebook
https://developers.facebook.com/apps

Twitter
https://dev.twitter.com/apps

Google
https://code.google.com/apis/console/

Yahoo!
https://developer.apps.yahoo.com/projects

Microsoft
http://msdn.microsoft.com/en-us/live/

Once you’ve got all that filled out, add the following to your settings file with their respective values:


UPDATE… links to all articles…

Facebook – https://chriskief.com/2012/12/22/django-register-with-oauth-facebook/
Twitter – https://chriskief.com/2012/12/22/django-register-with-oauth-twitter/
Google – https://chriskief.com/2012/12/22/django-register-with-oauth-google/
Yahoo! – https://chriskief.com/2012/12/30/django-register-with-oauth-yahoo/
Microsoft – https://chriskief.com/2013/01/05/register-with-oauth-microsoft-live/