Question

I'm starting a project with Django and I'm trying to allow users log in with Facebook. For the site purposes, I want to store the user likes in my database.

I took as an example the example app on Python Social Auth (the library I'm using for the project) and I've written a pipeline for storing data in DB.

The problem comes when I try to access likes, because the data that the response I get just gives me the basic data, so the following code doesn't work.

def user_details(strategy, details, response, user=None, *args, **kwargs):
    attrs = {'user': user}
    if strategy.backend.__class__.__name__ == 'FacebookOAuth2':
        #no response['likes']
        for i in response['likes']:
            for d in i['data']:
                p = UserPreferences(user=user,like=d['name'])
                p.save()

My app in Facebook asks for likes permissions, and I have the following on my settings.py (with the secret and all that stuff):

SOCIAL_AUTH_SCOPE = ['user_likes']

Any way to make this work?

Was it helpful?

Solution

Rename the setting SOCIAL_AUTH_SCOPE to SOCIAL_AUTH_FACEBOOK_SCOPE. Likes aren't sent automatically in the auth process, you need to query them to Facebook API, for example:

def get_likes(strategy, details, response, *args, **kwargs):
    if strategy.backend.name == 'facebook':
        likes = strategy.backend.get_json(
            'https://graph.facebook.com/%s/likes' % response['id'],
            params={'access_token': response['access_token']}
        )
        for like in likes['data']:
            pass  # Process and save likes here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top