Python Social Auth with Django – How to retrieve additional data?

Python Social Auth with Django – How to retrieve additional data? … here is a solution to the problem.

Python Social Auth with Django – How to retrieve additional data?

For my first Django project, I wanted to use Python Social Auth for social authentication (i.e. Facebook).
Django==2.0
Social authentication app – django==2.1.0
Social Authentication Core ==1.7.0

How do I retrieve extra data from a signed-in user’s profile? My goal is to filter logged-in users to custom groups based on the FB groups they belong to. But at this point I can’t even receive the email, only 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 pipe is the base pipe:

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 don’t ask for the full code, any help would be appreciated.

Thanks in advance!

Solution

python-social-auth will only store the basic user information needed to populate the model fields, and if any extra content is part of the authentication payload from the provider, and it’s set in the EXTRA_DATA, it will also be stored in the socially-related class as part of the extra_data property.

However, python-social-auth doesn’t call any other APIs in the provider to get extra data, and for that, you’ll need to enhance PIPELINE's use of methods that will call these additional endpoints on Facebook, and once you have a response, you can store it in a suitable case for your project.

To debug what the provider provides by default, add a debug pipeline (social_core.pipeline.debug.debug) between steps. If what you’re looking for is already part of the payload, note the key name and add it to the EXTRA_DATA settings. If not, you’ll need to add a method to call the Facebook API to retrieve extra information.

Related Problems and Solutions