You must first create OAuth credentials for Google +.
- Go to the Google Developer Console
- Create a new project.
- Go to "API and Authentication" → "Login Screen" and give your product a name. Click "Save."
- Go to "APIs and Authentication" → "Credentials". In the OAuth section, click Create a new customer ID. Add " http: // localhost: 8000 / soc / complete / google-oauth2 / " should be specified as the callback URL. This will only work for testing, make sure you enter your actual domain during production.
Now add python-social-auth to your Django application.
- Install
python-social-auth with pip Set the appropriate Django settings:
- Add
'social.apps.django_app.default' to INSTALLED_APPS : - Add the settings
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY and SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET using the client key and the secret that you created earlier. The customer’s key is the Customer’s Identifier, which appears on the Credentials screen of the Google Developer Console, ending in .apps.googleusercontent.com. Just take part before the point. The secret is listed as "Client Secret". Make sure the AUTHENTICATION_BACKENDS parameter AUTHENTICATION_BACKENDS explicitly defined and contains 'social.backends.google.GoogleOAuth2' . An example is:
AUTHENTICATION_BACKENDS = ( 'social.backends.google.GoogleOAuth2', 'django.contrib.auth.backends.ModelBackend')
Define the SOCIAL_AUTH_PIPELINE parameter as described in the python-social-auth documentation . All settings are indicated on this 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":
where user is the django.contrib.auth.models.User object and response is a dictionary. Then add this function to SOCIAL_AUTH_PIPELINE using the full module path after create_user .
If you do not want to do anything with this information, you can leave the default pipeline as is.
Finally, you'll want to add the python-social-auth urlpatterns to your urlpatterns site:
from django.conf.urls import include url("^soc/", include("social.apps.django_app.urls", namespace="social"))
And it must be! This is the time to test. First, ./manage.py makemigrations for the required python-social-auth migrations, and then ./manage.py migrate , as described here . Then you can start the development server and go to http: // localhost: 8000 / soc / login / google-oauth2 /? Next = / .
I hope I haven’t missed an explanation of any step and it will work. Feel free to ask more questions and read documents . Also, here is a working example that you should check.
rhaps0dy Mar 17 '15 at 10:17 2015-03-17 10:17
source share