Created
September 28, 2011 18:12
-
-
Save dchaplinsky/1248728 to your computer and use it in GitHub Desktop.
Get avatars from social networks (facebook and google) with django-social-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from social_auth.backends.facebook import FacebookBackend | |
from social_auth.backends import google | |
def social_extra_values(sender, user, response, details, **kwargs): | |
result = False | |
if "id" in response: | |
from apps.photo.models import Photo | |
from urllib2 import urlopen, HTTPError | |
from django.template.defaultfilters import slugify | |
from apps.account.utils import user_display | |
from django.core.files.base import ContentFile | |
try: | |
url = None | |
if sender == FacebookBackend: | |
url = "http://graph.facebook.com/%s/picture?type=large" \ | |
% response["id"] | |
elif sender == google.GoogleOAuth2Backend and "picture" in response: | |
url = response["picture"] | |
if url: | |
avatar = urlopen(url) | |
photo = Photo(author = user, is_avatar = True) | |
photo.picture.save(slugify(user.username + " social") + '.jpg', | |
ContentFile(avatar.read())) | |
photo.save() | |
except HTTPError: | |
pass | |
result = True | |
return result | |
pre_update.connect(social_extra_values, sender=None) |
i have to show the profile image from google while login in django from the template . Anyone , please help me?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have redone your code to fetch the photo upon registration. https://gist.github.com/1662930