首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >get Google avatar

get Google avatar
EN

Stack Overflow用户
提问于 2014-09-08 23:29:26
回答 4查看 4.5K关注 0票数 5

有没有办法使用python社交网站获取谷歌个人资料图片url?

所以,这就是我在facebook和twitter上所做的,用下面的代码添加一个管道:

代码语言:javascript
复制
    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来保存配置文件的化身?是这条路吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-09-09 12:53:46

代码语言:javascript
复制
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()
票数 5
EN

Stack Overflow用户

发布于 2015-10-16 01:52:15

要从社交登录获得化身,您需要在应用程序中创建一个pipeline.py文件,并将这一行添加到settings.py中:

代码语言:javascript
复制
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文件中。

代码语言:javascript
复制
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()
票数 2
EN

Stack Overflow用户

发布于 2014-09-09 03:41:28

我不熟悉Python,但是看起来您需要GooglePlusAuth

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25734611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档