Python Social Auth如何获取Google头像

5

有没有一种方法可以使用Python社交身份验证获取Google个人资料图片URL?

所以,这是我在Facebook和Twitter上执行的操作,添加一个管道与以下代码:

    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 = "???"

首先,我不知道后端的名称是什么,它是“GoogleOAuth2”吗? 其次,我应该使用哪个URL来保存个人资料的头像?这是正确的方式吗?

4个回答

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()

谢谢vero4ka。我不想保存图片,只想保存URL,所以url_50 = response['image'].get('url')解决了问题。希望Google不会很快改变URL的格式。 - Alejandro Veintimilla

2
为了从社交登录中获取头像,您需要在应用程序中创建一个pipeline.py文件,并将以下行添加到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.
)

并在后续将此内容添加到您的pipeline.py文件中。

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()

欢迎来到SO。如果您不介意的话,应该为您的代码添加一些解释。或者至少加上注释。您可以查看此元问题有关仅限代码答案的信息。 - GabrielOshiro

0

我正在使用vero4ka建议的方法。

在代码行ext = url.split('.')[-1]附近插入一个Python调试器(import pdb; pdb.set_trace()),我注意到split函数的输出并没有完全分离文件扩展名:

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

我需要在问号?符号处进行分割。没什么大不了的。

不确定为什么要以这种方式拆分然后重新组合扩展名?


0

我不熟悉Python Social Auth,但看起来你想要GooglePlusAuth

要获取图像URL,您需要使用userId设置为me并作为用户进行身份验证,向people.get API方法发出HTTP请求。响应包括一个image.url值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接