Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active December 10, 2018 21:03
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cansadadeserfeliz/9899590 to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/9899590 to your computer and use it in GitHub Desktop.
Django: python-social-auth configuration and pipeline to get users email, full name and save avatar for Facebook, Twitter, LinkedIn and Google http://blog.vero4ka.info/blog/2015/03/02/django-python-social-auth-configuration-and-pipeline-to-get-users-information/
def update_user_social_data(strategy, *args, **kwargs):
"""Set the name and avatar for a user only if is new.
"""
print 'update_user_social_data ::', strategy
if not kwargs['is_new']:
return
full_name = ''
backend = kwargs['backend']
user = kwargs['user']
if (
isinstance(backend, GoogleOAuth2)
or isinstance(backend, FacebookOAuth2)
):
full_name = kwargs['response'].get('name')
elif (
isinstance(backend, LinkedinOAuth)
or isinstance(backend, TwitterOAuth)
):
if kwargs.get('details'):
full_name = kwargs['details'].get('fullname')
user.full_name = full_name
if isinstance(backend, GoogleOAuth2):
if response.get('image') and response['image'].get('url'):
url = response['image'].get('url')
ext = url.split('.')[-1]
user.avatar.save(
'{0}.{1}'.format('avatar', ext),
ContentFile(urllib2.urlopen(url).read()),
save=False
)
elif isinstance(backend, FacebookOAuth2):
fbuid = kwargs['response']['id']
image_name = 'fb_avatar_%s.jpg' % fbuid
image_url = 'http://graph.facebook.com/%s/picture?type=large' % fbuid
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
elif isinstance(backend, TwitterOAuth):
if kwargs['response'].get('profile_image_url'):
image_name = 'tw_avatar_%s.jpg' % full_name
image_url = kwargs['response'].get['profile_image_url']
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
elif isinstance(backend, LinkedinOAuth):
if kwargs['response'].get('pictureUrl'):
image_name = 'linked_avatar_%s.jpg' % full_name
image_url = kwargs['response'].get['pictureUrl']
image_stream = urlopen(image_url)
user.avatar.save(
image_name,
ContentFile(image_stream.read()),
)
user.save()
# python-social-auth
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.twitter.TwitterOAuth',
'social.backends.linkedin.LinkedinOAuth',
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.mail.mail_validation',
'social.pipeline.social_auth.associate_by_email',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'myapp.pipeline.update_user_social_data',
)
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_STRATEGY = 'social.strategies.django_strategy.DjangoStrategy'
SOCIAL_AUTH_STORAGE = 'social.apps.django_app.default.models.DjangoStorage'
SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''
SOCIAL_AUTH_LINKEDIN_KEY = ''
SOCIAL_AUTH_LINKEDIN_SECRET = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
SOCIAL_AUTH_LINKEDIN_SCOPE = ['r_basicprofile', 'r_emailaddress']
# These fields be requested from linkedin.
SOCIAL_AUTH_LINKEDIN_FIELD_SELECTORS = [
'email-address',
'picture-url',
]
SOCIAL_AUTH_LINKEDIN_EXTRA_DATA = [
('id', 'id'),
('firstName', 'first_name'),
('lastName', 'last_name'),
('emailAddress', 'email_address'),
('public-profile-url', 'public_profile_url'),
]
@ymorin007
Copy link

Thanks for the script ... I was wondering if you had an update version with Yahoo.

Because I get this error with Yahoo:
if "id" in kwargs['response']:
TypeError: argument of type 'SuccessResponse' is not iterable

@FrankMillers
Copy link

Thanks for the script

@drewshaver
Copy link

The google one didn't work for me, looks like the new key is ['image']['url']

@rbtsolis
Copy link

Thank you!

@franlu
Copy link

franlu commented Dec 30, 2015

Thank you so much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment