python-social-auth quick start instructions for Django

3,476 views
Skip to first unread message

Paulo Eduardo Neves

unread,
Oct 10, 2013, 7:06:01 PM10/10/13
to django-so...@googlegroups.com
Hi,
I'm trying to configure python-social-auth to my django site, but the installation instructions are really lacking. I'm full of doubts. I'd like to narrate my lost afternoon. Maybe someone can help me and improve the experience for new developers.

I want to configure Facebook, Google and email registration to my hobby site. Later I'd like to integrate it with the already existent user accounts that were created with another web framework.  The site has just one section using django, but this is a first and crucial step to migrate everything. I've just upgraded my Django to version 1.5.4.

I'm an experienced python developer, but almost a newbie in Django, and a complete newbie in OAuth. 

First, I've thought to install django-social-auth, but the docs say that it is deprecated and that I should use python-social-auth. 

Ok, I've installed it in my virtualenv with "pip install python-social-auth". Version 0.1.14. I've got new files under my lib/site-packages. Now the first confusion. The Get A Copy has some "and" and "or" instructions without parentheses. If I had to review a code like this, I'd ask for some parentheses to avoid ambiguity. I decided to just execute the pip install and follow on. 


I really don't know which options have sensible defaults and which ones I have to fill. BTW, I don't even know where I have to edit these setting. I believe they should be set in the project/settings.py file. 

There isn't a list with all of them. I've found the modules in lib/site-packages/social/backends, but I have to enter in each one to discover the class name I should use. I also have no idea about the advantages or disadvantages of GoogleOAuth, GoogleOAuth2 or GoogleOpenId. To get the key and secret inside Google site is really confusing. Facebook is a little better, but I'm not really sure if all the necessary options are filled in their site.

Then we get in the User Model section:
UserSocialAuth instances keep a reference to the User model of your project, since this is not know, the User model must be configured by a setting:
SOCIAL_AUTH_USER_MODEL = 'foo.bar.User'

I don't believe that foo.bar.User is a correct value. Shouldn't  the app create this table? I'm lost here. Let's ignore it.

is incomprehensible. I've set 223 to one of the fields, since I'm using MySQL, and invented some sums for the other fields. 

All the sections are confusing. Processing redirects looks really important, but since I don't know how to fill the, just  leave the defaults.

Now lets enter the Django configuration http://psa.matiasaguirre.net/docs/configuration/django.html
I thought I were there already. Again the problem to discover the authentication backends classes.  I'm really confused. 

Before the configuration option was SOCIAL_AUTH_AUTHENTICATION_BACKENDS now it is AUTHENTICATION_BACKENDS. I've just noticed the difference while writing this message. Initially I've used the SOCIAL_ prefix. 

Template Context Processors aren't in my Django settings. I'm using the default. I've copied the default and added the two new options at the end. I've no idea what are the backends talked about in the documentation. Hope they won't bite me in the future. 

Exceptions Middleware looks important, but I'll leave it quiet since I don't know why I should throw exceptions or not. How would I override the mentioned methods? Should I subclass the middleware? Well, I've seen a middleware option in my settings file, I'll add social.apps.django_app.middleware.SocialAuthExceptionMiddleware to it.
Hope I can add it anywhere.

Well, now I'm lost. There are a lot of templates in site-packages but I didn't touch them. the urls where added to my django, but accessing /login fails. I try to access my admin page, and the old authentication opens. 

Would someone orient me? I'm  stucked.

kind regards,
Paulo


David Koppstein

unread,
Oct 22, 2013, 9:29:09 PM10/22/13
to django-so...@googlegroups.com, paulo...@gmail.com
+1. I appreciate the work that's been done so far, but it's still very confusing. After a lot of trial and error I managed to install the models, but it's unclear as to how to integrate them with the rest of the website in any meaningful way, i.e. to get a login screen.

Matías Aguirre

unread,
Oct 22, 2013, 10:21:31 PM10/22/13
to David Koppstein, django-social-auth, pauloneves
If you are working with Django, then almost all the information you need should
be located at http://psa.matiasaguirre.net/docs/configuration/django.html.
I said almost since there are important bits in the main configuration section
http://psa.matiasaguirre.net/docs/configuration/settings.html like "Keys and
secrets".

From Paulo emails I think that most of the confusion comes from the
inexperience with Django and the advanced stuff that this application depends
on like custom user models, middlewares and context processors.

Of course that PSA docs could, should and must be improved, and part of the
issue is that these docs aren't written to newer Django adopters (which should
be taken into account too).

There are many things to improve the library, like code, utilities and docs and
I'm working hard on them, and any contribution is welcome. I've planed some
screencasts detailing the process to setup the app for a while, but no time so
far.

Paulo and David, would you like to join to an IRC session or maybe a Google
Hangout to do an step by step guide?

Thanks,
Matías

Excerpts from David Koppstein's message of 2013-10-22 23:29:09 -0200:
> +1. I appreciate the work that's been done so far, but it's still very
> confusing. After a lot of trial and error I managed to install the models,
> but it's unclear as to how to integrate them with the rest of the website
> in any meaningful way, i.e. to get a login screen.
>
> On Thursday, October 10, 2013 7:06:01 PM UTC-4, Paulo Eduardo Neves wrote:
> >
> > Hi,
> > I'm trying to configure python-social-auth to my django site, but the
> > installation instructions are really lacking. I'm full of doubts. I'd like
> > to narrate my lost afternoon. Maybe someone can help me and improve the
> > experience for new developers.
> >
> > I want to configure Facebook, Google and email registration to my hobby
> > site. Later I'd like to integrate it with the already existent user
> > accounts that were created with another web framework. The site has just
> > one section using django, but this is a first and crucial step to migrate
> > everything. I've just upgraded my Django to version 1.5.4.
> >
> > I'm an experienced python developer, but almost a newbie in Django, and a
> > complete newbie in OAuth.
> >
> > First, I've thought to install django-social-auth, but the docs say that
> > it is deprecated and that I should use python-social-auth.
> >
> > Ok, I've installed it in my virtualenv with "pip install
> > python-social-auth". Version 0.1.14. I've got new files under my
> > lib/site-packages. Now the first confusion. The Get A Copy<http://psa.matiasaguirre.net/docs/installing.html#get-a-copy> has
> > some "and" and "or" instructions without parentheses. If I had to review a
> > code like this, I'd ask for some parentheses to avoid ambiguity. I decided
> > to just execute the pip install and follow on.
> >
> > Now, there is the settings section:
> > http://psa.matiasaguirre.net/docs/configuration/settings.html
> >
> > I really don't know which options have sensible defaults and which ones I
> > have to fill. BTW, I don't even know where I have to edit these setting. I
> > believe they should be set in the project/settings.py file.
> >
> > Even worse are the authentication backends
> > http://psa.matiasaguirre.net/docs/configuration/settings.html#authentication-backends
> > There isn't a list with all of them. I've found the modules in
> > lib/site-packages/social/backends, but I have to enter in each one to
> > discover the class name I should use. I also have no idea about the
> > advantages or disadvantages of GoogleOAuth, GoogleOAuth2 or GoogleOpenId.
> > To get the key and secret inside Google site is really confusing. Facebook
> > is a little better, but I'm not really sure if all the necessary options
> > are filled in their site.
> >
> > Then we get in the User Model section:
> >
> >> UserSocialAuth instances keep a reference to the User model of your
> >> project, since this is not know, the User model must be configured by a
> >> setting:
> >> SOCIAL_AUTH_USER_MODEL = 'foo.bar.User'
> >
> >
> > I don't believe that foo.bar.User is a correct value. Shouldn't the app
> > create this table? I'm lost here. Let's ignore it.
> >
> > The section *Tweaking some fields length *
--
Matías Aguirre (matias...@gmail.com)

David Koppstein

unread,
Oct 22, 2013, 10:25:24 PM10/22/13
to Matías Aguirre, django-social-auth, pauloneves
Sure, I'd be happy to participate in an IRC or google hangout and would
be happy to contribute to the docs, especially from the perspective of a
newcomer to Django.

David

Dennis

unread,
Nov 5, 2013, 4:25:27 AM11/5/13
to django-so...@googlegroups.com, Matías Aguirre, pauloneves
Just wondering if this IRC or google hangout has already happened.
If not, can others join in?
It would be great to have this help session available as a recording ... it's better than having nothing and can be an intermediate solution until the more official webcasts are made.

Matías Aguirre

unread,
Nov 5, 2013, 9:24:45 AM11/5/13
to Dennis, django-social-auth, pauloneves
It didn't happen yet. The hard part is to coordinate the hour. I want this to
be done on in an OnAir hangout, so it gets recorded automatically.

Excerpts from Dennis's message of 2013-11-05 07:25:27 -0200:
--
Matías Aguirre (matias...@gmail.com)

Matías Aguirre

unread,
Nov 5, 2013, 1:29:50 PM11/5/13
to Paulo Eduardo Neves, Dennis, django-social-auth
Hey,

I've registered this event to track the hangout:

https://beta.homago.com/webmaking/geekout/87/python-social-auth-beginners

Please, register to the site and grab a spot in the event, let me know if the
time works, I can update it to fit as mas people as possible.

Excerpts from Paulo Eduardo Neves's message of 2013-11-05 15:58:51 -0200:
> Ops, I was sick and lost the initial messages. Count me in the hangout. I'm
> in GMT-02. I won't be able to do it during work hours.
>
> regards,
> Paulo
--
Matías Aguirre (matias...@gmail.com)

Avi Alkalay

unread,
Apr 19, 2014, 11:17:50 PM4/19/14
to django-so...@googlegroups.com, paulo...@gmail.com
I'm also trying to use this library with very poor success. Eventually I'll have to write a backend for the Moves app.

Not sure whom the documentation was written for. It is too theorical, code snippets doesn't inform the context or files, zero practical examples, awful. Sorry.

I was able to make an empty Django project authenticate with Google and actually create user info in the database. Not that I know how to use it from there, but I'll share here what I did, for the poor fellows that are on the same boat as me:
  1. I'm on Fedora Linux 20, so python-social-auth was installed from the rawhide/f21 repo with yum

  2. django-admin startproject djangoauthtest

  3. Create and app/client on Google Developers Console of type Service Account and set the Redirect URI to http://djangoauthtest.com:8081/complete/google-oauth2/. The "google-oauth2" name was found on https://github.com/omab/python-social-auth/blob/master/social/backends/google.py as "name = 'google-oauth2'" inside class GoogleOAuth2. You will have to look at desired backend's source code to see how each is named. Don't expect similar homogeneous names for other backends. Facebook, for example, is "facebook-app".

  4. On djangoauthtest/djangoauthtest/settings.py I have:
    INSTALLED_APPS = (
    .....
        # Uncomment the next line to enable the admin:
        'django.contrib.admin',
        'social.apps.django_app.default',
        # Uncomment the next line to enable admin documentation:
        # 'django.contrib.admindocs',
    )

    AUTHENTICATION_BACKENDS = (
          'social.backends.google.GoogleOAuth2',
          'django.contrib.auth.backends.ModelBackend'
      )

    SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '218.....39.apps.googleusercontent.com'
    SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '52tq.........0a5wnyv'
    SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = ['https://www.googleapis.com/auth/calendar']

  5. On djangoauthtest/djangoauthtest/settings.py I uncommented the lines to activate the admin interface (from Django documentation) and included the following:
    url('', include('social.apps.django_app.urls', namespace='social'))

  6. python manage.py syncdb

  7. python manage.py runserver 0.0.0.0:8081

  8. Then access http://djangoauthtest.com:8081/login/google-oauth2/ which will redirect you to Google and ask for your permission and redirect you back to your Redirect URI

  9. Then check http://djangoauthtest.com:8081/admin/default/usersocialauth/ (the Django admin interface) and other tables, how credentials were created.

I noticed Google complains if the URL of your site (http://djangoauthtest.com:8081) is not publicly accessible. My first try was with something like http://192.168.x.y:8081 and Google failed saying something like client_id was required. Didn't try harder, I have to confess.

Good luck !

Avi Alkalay

unread,
Apr 19, 2014, 11:22:36 PM4/19/14
to django-so...@googlegroups.com, paulo...@gmail.com
On step 5 of my previous post I was talking about urls.py file, not settings.py.

On Sunday, April 20, 2014 12:17:50 AM UTC-3, Avi Alkalay wrote:
5. On djangoauthtest/djangoauthtest/settings.py I uncommented the lines to activate the admin interface (from Django documentation) and included the following:

Matías Aguirre

unread,
Apr 22, 2014, 7:12:58 PM4/22/14
to Avi Alkalay, django-social-auth, pauloneves
This was published recently, take a look to it, it might be the
tutorial everybody was looking for...

http://www.artandlogic.com/blog/2014/04/tutorial-adding-facebooktwittergoogle-authentication-to-a-django-application/

Matías

Excerpts from Avi Alkalay's message of 2014-04-20 00:22:36 -0300:
--
Matías Aguirre (matias...@gmail.com)
Reply all
Reply to author
Forward
0 new messages