Search code examples
pythondjangofacebookpython-social-auth

Python Social Auth with Django - how to retrieve extra data?


For my first Django project I wanted to tie up Python Social Auth for the social authentication (namely Facebook).
Django==2.0
social-auth-app-django==2.1.0
social-auth-core==1.7.0

How can I retrieve extra data from the logged in user's profile? My goal is to filter the logged in users to custom groups based on FB groups they are members of. However at this point I can't even get the email, just the username.

In my settings.py:

SOCIAL_AUTH_FACEBOOK_KEY = 'xxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxx'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', 'groups_access_member_info']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    'fields': 'id, name, email',
    'edges': 'groups'
}

The pipeline is the basic pipline:

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
)

I'm not asking for complete code, any help would be much appreciated.

Thank you in advance!


Solution

  • python-social-auth will just store the basic user information it needs to fill the model fields, if anything extra is part of the auth payload from the provider, and it's configured in the EXTRA_DATA setting, it will also be stored in the social-related class as part of the extra_data attribute.

    Still, python-social-auth won't call any other API in the provider to fetch additional data, for that to work, you need to enhance the PIPELINE with your methods that will call these additional endpoints on Facebook, once with the response, you can store it were it fits on your project.

    To debug what's coming by default from the provider, add the debug pipeline between the steps (social_core.pipeline.debug.debug). If what you are looking for is already part of the payload, then take note of the key name and add it to the EXTRA_DATA setting. If it's not, then you need to add a method that will call Facebook API to retrieve the extra information.