Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

oauth - How to sign in with the Google+ API using Django?

How might I go about adding a Google+ API sign-in to my Django website?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you must create OAuth credentials for Google+.

  1. Go to the Google Developer Console
  2. Create a new project.
  3. Go to "APIs and authentication" -> "Authorization screen" and give your product a name. Click "Save".
  4. 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.

  1. Install python-social-auth with pip
  2. Set the appropriate Django settings:

    • Add 'social.apps.django_app.default' to INSTALLED_APPS:
    • Add the SOCIAL_AUTH_GOOGLE_OAUTH2_KEY and SOCIAL_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 a django.contrib.auth.models.User object, and response is a dictionary. Then add that function to the SOCIAL_AUTH_PIPELINE using the full module path, after create_user.

    If you don't want to do anything with that information you can leave the default pipeline as-is.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...