How might I go about adding a Google+ API sign-in to my Django website?
-
How to do sign-out using Google oauth in python? – Menu Jul 21 '17 at 09:21
2 Answers
First you must create OAuth credentials for Google+.
- Go to the Google Developer Console
- Create a new project.
- Go to "APIs and authentication" -> "Authorization screen" and give your product a name. Click "Save".
- Go to "APIs and authentication" -> "Credentials". Under "OAuth", click "Create New Client ID". Add "http://localhost:8000/soc/complete/google-oauth2/" should be listed as a callback URL. This will only work for testing, make sure to put in your actual domain when in production.
Now let's add python-social-auth
to your Django app.
- Install
python-social-auth
withpip
Set the appropriate Django settings:
- Add
'social.apps.django_app.default'
toINSTALLED_APPS
: - Add the
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY
andSOCIAL_AUTH_GOOGLE_OAUTH2_SECRET
settings with the client key and secret you created earlier. The client key is the "Client ID" listed in the "Credentials" screen in the Google developer console, the one which ends in ".apps.googleusercontent.com". Only take the part before the dot. The secret is listed as "Client secret". Make sure you have the
AUTHENTICATION_BACKENDS
setting explicitly defined, and that it contains'social.backends.google.GoogleOAuth2'
. An example would be:AUTHENTICATION_BACKENDS = ( 'social.backends.google.GoogleOAuth2', 'django.contrib.auth.backends.ModelBackend')
Define the
SOCIAL_AUTH_PIPELINE
setting as detailed in the python-social-auth documentation. What every setting does is listed in that page.
If you have something to do with the information you get from Google+, I recommend defining a function:
def save_profile(backend, user, response, *args, **kwargs): if backend.name == "google-oauth2": # do something
where
user
is adjango.contrib.auth.models.User
object, andresponse
is a dictionary. Then add that function to theSOCIAL_AUTH_PIPELINE
using the full module path, aftercreate_user
.If you don't want to do anything with that information you can leave the default pipeline as-is.
- Add
Finally, you'll want to add the python-social-auth
urls to your site's urlpatterns
:
from django.conf.urls import include
url("^soc/", include("social.apps.django_app.urls", namespace="social"))
And that should do it! It's time for testing. First, ./manage.py makemigrations
for the required migrations of python-social-auth
, and then ./manage.py migrate
, as explained here. Then, you can run the development server, and go to http://localhost:8000/soc/login/google-oauth2/?next=/ .
Hopefully I did not skip explaining any step and it will work. Feel free to ask more questions and read the docs. Also, here is a working example that you should check out.

- 81,660
- 23
- 145
- 268

- 1,236
- 11
- 13
-
2i have followed all steps, its working as the user login using social auth but my user is not getting created , its just login , though i see a new record is created in table "social_auth_usersocialauth" but the no new user is created in User table. i am extending the AbstractBaseUser and created my custom user domain. any help would be appreciated – roanjain Apr 28 '16 at 07:14
-
1I wish I could up vote this answer many times... I've read 3 tutorials and only after reading this I got it working. – OMRY VOLK May 14 '16 at 13:24
-
6Also make sure to add the Google+ API to the list of enabled APIs on the Google Developer Console (under APIs), otherwise you'll get a 403 – spg Sep 23 '16 at 16:19
-
Where does `include` come from? In a fresh Django + DRF + social-auth, that is not a predefined function, and `manage.py` runs crash when they hit it. – Mike 'Pomax' Kamermans Nov 22 '16 at 20:20
-
2`include` comes from `django.conf.urls`. https://docs.djangoproject.com/en/1.10/ref/urls/ – rhaps0dy Jan 18 '17 at 10:56
-
6Note that at some point after this answer was posted `python-social-auth` was changed over to a new library structure; it is now `social-auth-core`, with dedicated apps for specific frameworks, so in this case `social-auth-app-django`. – Mike 'Pomax' Kamermans Apr 27 '17 at 18:21
-
-
This works for the GoogleOAuth2, but not for GooglePlusAuth backend. – pilotandy Jan 30 '18 at 02:07
-
I am getting AuthFailureError, I am calling this: http://
/soc/login/google-oauth2/?next=/ I used POST and GET methods (With GET I am getting 302) – Jesus Almaral - Hackaprende Sep 15 '19 at 22:45 -
1In the browser I am getting this Error: redirect_uri_mismatch The redirect URI in the request, http://
/auth/complete/google-oauth2/, does not match the ones authorized for the OAuth client. To update authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/... – Jesus Almaral - Hackaprende Sep 15 '19 at 23:17 -
Some new notice. In `AUTHENTICATION_BACKENDS` should be `social_core` instead of `social` – Богуслав Павлишинець Aug 04 '20 at 08:50
@rhaps0dy's answer is correct, but python-social-auth
is now deprecated and migrated as social-auth-app-django
. So this is what I made different from @rhaps0dy guidelines.
- Instead of
python-social-auth
, I installedsocial-auth-app-django
, 'social.apps.django_app.default'
becomes'social_django'
'social.backends.google.GoogleOAuth2'
is now'social_core.backends.google.GoogleOAuth2'
url("^soc/", include("social.apps.django_app.urls", namespace="social"))
becomesurl("^soc/", include("social_django.urls", namespace="social"))

- 1
- 1

- 1,215
- 10
- 10
-
3
-
2Thank you both! Pretty clean answer and it's working with modifications mentioned by georgi – Filipe Spindola Apr 20 '17 at 14:41
-
1