Get user data using django-social-auth

Recently we had to add support for social networks login to an application we are developing and we chose django-social-auth to work with. It is a well documented and easy to use django application for authentication.

But we wanted to do more than just authenticating the user, we wanted to get extra data like the profile picture, gender, etc. Fortunately django-social-auth has an useful pipeline that you can expand to fulfill this kind of tasks. I will explain briefly how to achieve this. I will assume you already have authentication working which is very well explained in the documentation.

First of all, we should define the pipeline in our like this:


all the items in the pipeline except the last one are from django-social-auth and are part of the original pipeline, and the last one is the one in charge of getting the avatar and should be coded.

After we have our pipeline ready we need to implement the function that looks like this:

def get_user_avatar(backend, details, response, social_user, uid,
                    user, *args, **kwargs):
    url = None
    if backend.__class__ == FacebookBackend:
        url = "" % response['id']

    elif backend.__class__ == TwitterBackend:
        url = response.get('profile_image_url', '').replace('_normal', '')

    if url:
        profile = user.get_profile()
        avatar = urlopen(url).read()
        fout = open(filepath, "wb") #filepath is where to save the image
        fout.close() = url_to_image # depends on where you saved it

If you want to extend the functionality of getting the avatar from others social networks you should check the backend and get the image url accordingly.

On the other hand, if you want to get another kind of information like gender or age you should define another pipeline item and do something like we did above.

That’s pretty much everything. Happy coding!

We are Tryolabs. A boutique hi-tech company that creates amazing Internet & Mobile products.

8 thoughts on “Get user data using django-social-auth

  1. Awesome. Just about to dive deep into SNA and hopefully some Machine Learning, and NLP. Looks like django-social-auth is a great starting point. Thanks for the great post.

  2. Nice starting point, but i guess something is missing (not sure indeed).
    First of all the imports, which not all the people knows.
    Second how the "profile" model is made and the fact that you have to write the AUTH_PROFILE_MODULE accordingly to the "profile" you have.
    Am I right?

    1. Hi!

      In the SOCIAL_AUTH_PIPELINE you can see the path to the function: ‘auth_pipelines.pipelines.get_user_avatar’. In this case I had a module (in the root of the project) named “auth_pipelines” with a file named “pipelines” that inside had the “get_user_avatar” function. Hope this helps, otherwise ask me again.

  3. What if we want to show some form to user after creating the user profile, so that we can update the existing profile? How to manage that flow?

  4. Hi,
    I m trying to login via fb in dhango,everything is working fine.problem is i m not getting email id of user.i have added SOCIAL_AUTH_FACEBOOK_SCOPE = [’email’] to not getting email which is important for my database,plz help.
    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *