Python Social Auth get Google avatar
Asked Answered
H

4

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?

Haematocryal answered 8/9, 2014 at 23:29 Comment(0)
F
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()
Fingerbreadth answered 9/9, 2014 at 12:53 Comment(1)
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.Haematocryal
G
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()
Gingergingerbread answered 16/10, 2015 at 1:52 Comment(1)
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 answersBurr
S
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.

Sayles answered 9/9, 2014 at 3:41 Comment(0)
C
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?

Cordite answered 20/11, 2014 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.