Python社交认证获取Goog

2024-04-19 17:18:21 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有一种方法可以使用pythonsocial auth获取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”吗? 第二,我应该用什么网址来保存个人资料的头像?。是这条路吗?在


Tags: 方法nameimageidbackendurlfacebookif
3条回答
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()

我不熟悉Python的Social Auth,但看起来您想要GooglePlusAuth。在

要获得图像url,您必须向people.get API method发出HTTP请求,并将userId设置为me,并作为用户进行身份验证。响应包含一个image.url值。在

要从社交登录获取头像,您需要创建一个管道.py在你的应用程序中归档并将这些行添加到设置.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.
)

然后将这些内容添加到管道.py文件

^{pr2}$

相关问题 更多 >