83đ
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.
43đ
@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"))
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- [Django]-How use Django with Tornado web server?
- [Django]-Get virtualenv's bin folder path from script