5

Is there a way to get the Google profile picture url using python social auth?

So, this is what I do for facebook and twitter, add a pipeline with this code:

    if strategy.backend.name == 'facebook':
        url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])

    elif strategy.backend.name == "twitter":
        if response['profile_image_url'] != '':
            url = response['profile_image_url']

    elif strategy.backend.name == "GoogleOAuth2":  # doesn't work
        user_id = response['id']
        url = "???"

First, I don't know what is the name of the backend, is it "GoogleOAuth2"? Second, what is the url I should use to save the profile's avatar?. Is this the way?

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

4 Answers4

5
from social.backends.google import GoogleOAuth2

def save_profile(backend, user, response, *args, **kwargs):
    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
            )
            user.save()
cansadadeserfeliz
  • 3,033
  • 5
  • 34
  • 50
  • Thanks vero4ka. I didn't want to save the image, only the url so `url_50 = response['image'].get('url')` did the trick. Hope that google doesn't change the url format anytime soon. – Alejandro Veintimilla Sep 09 '14 at 20:39
2

To get avatars from social login, you need create a pipeline.py file in your app and add this lines to settings.py:

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.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',
    'apps.users.pipeline.get_avatar', # This is a path of your pipeline.py
    #and get_avatar is the function.
)

and later add this content to your pipeline.py file

def get_avatar(backend, strategy, details, response,
        user=None, *args, **kwargs):
    url = None
    if backend.name == 'facebook':
        url = "http://graph.facebook.com/%s/picture?type=large"%response['id']
        # if you need a square picture from fb, this line help you
        url = "http://graph.facebook.com/%s/picture?width=150&height=150"%response['id']
    if backend.name == 'twitter':
        url = response.get('profile_image_url', '').replace('_normal','')
    if backend.name == 'google-oauth2':
        url = response['image'].get('url')
        ext = url.split('.')[-1]
    if url:
        user.avatar = url
        user.save()
Roberth Solís
  • 1,520
  • 18
  • 16
  • Welcome to SO. If you don't mind, you should add an explanation to your code. Or comment it at least. You might check [this meta question on code-only answers](http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking-code-only-ques) – GabrielOshiro Oct 16 '15 at 02:15
0

I'm not familiar with Python Social Auth but it looks like you want GooglePlusAuth.

To get the image url you will have to make a HTTP request to the people.get API method with userId set to me and authenticated as the user. The response includes an image.url value.

abraham
  • 46,583
  • 10
  • 100
  • 152
0

I'm using the approach suggested by vero4ka.

Dropping into a python debug (import pdb; pdb.set_trace()) around the line ext = url.split('.')[-1], I notice that the split output doesn't fully separate the file extension viz:

(Pdb) url = response['image'].get('url').split('.')
(Pdb) url
[u'https://lh4', u'googleusercontent', u'com/redacted/redacted/photo', u'jpg?sz=50']

I'll need to split off on the ? symbol too. No big deal.

Not sure why one would split and then recombine the extension in this way?

GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
Chris McGinlay
  • 285
  • 2
  • 10