Django Social Auth:Get email from linkedin,twitter & facebook

1.8k Views Asked by At

I'm using Django social_auth api for login via social account. Here I want to get email address from social account and stored it in my database table. The first-name and last-name can be retrieved from the account but I can't retrieved email address, profile picture. Please share your ideas for retrieving those details from social account.

1

There are 1 best solutions below

0
On

Twitter doesn't disclose emails, to retrieve Facebook email you need to define FACEBOOK_EXTENDED_PERMISSIONS = ['email'], LinkedIn email is retrieved automatically. Emails are stored in the user model under the email attribute.

Profile picture can be stored by defining this settings:

TWITTER_EXTRA_DATA = [('profile_image_url', 'profile_picture')]
LINKEDIN_EXTRA_DATA = [('picture-url', 'profile_picture')]

Facebook profile picture is accessible by API and not sent during the auth processes. You can define your pipeline to store it like this:

from django.utils import simplejson
from social_auth.utils import dsa_urlopen

def facebook_profile_picture(backend, user, social_user, details, response, *args, **kwargs):
    if backend.name != 'facebook':
        return
    url = 'https://graph.facebook.com/{0}/picture?redirect=false&access_token={1}'
    response = dsa_urlopen(url.format(social_user.extra_data['id'], social_user.extra_data['access_token'])
    data = simplejson.load(response)
    social_user.extra_data['profile_picture'] = data['data']['url']
    social_user.save()

Add it to the bottom of the pipelines setting (check the pipelines doc at http://django-social-auth.readthedocs.org/en/latest/pipeline.html). This code wasn't tested, so play a little with it.

Then you can access the profile picture doing:

social = user.social_auth.get(provider='facebook')  # or twitter or linkedin
social.extra_data['profile_picture']