How to setup a Google Account Authentication

One of the awesome things that Django offers us is it lets us use our Google account as authentication to the website that we are building. I am going to show you simple steps to set it up.

Here are the steps:
1. install django-openid-auth in your virtualenv:
pip install django-openid-auth

2. install python-openid:
pip install python-openid

3. Open your “settings.py”.

4. add this to your installed apps: ‘django_openid_auth’

5. add these following setups:

6. open your urls.py in your project root.

7. Add this to your urlconf:

8. Create a file in your project root and name it auth.py.

9. Insert this code to the file you just created:

That’s it. Those are the steps you need to setup Google Account authentication.

Some information you need to know:

In step 7, we added the link to login using Google account and a logout link which logs out the user in the website. The login link which brings the user to Google Account login page looks like this “yoursite.com/login_openid”. If the user has successfully login with his Google account, it automatically redirects back to the site. The user is already logged in to “yoursite.com”. The logout link looks like this “yoursite.com/logout/”. This will logout the user to “yoursite.com”.

In the file auth.py, we have a class GoogleBackend with a function authenticate. The way authenticate function works here is it creates a user with the username equal to it’s google email. You can always modify the code according to your needs.

Source:
http://djangosnippets.org/snippets/2183/