首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python-social-auth中检索google和facebook的头像

在python-social-auth中检索google和facebook的头像
EN

Stack Overflow用户
提问于 2015-04-11 16:19:38
回答 4查看 7.4K关注 0票数 11

如何通过扩展管道来使用python-social-auth从google和facebook检索头像和出生日期?我读到过,我可以创建函数来这样做,并为它们设置路径,但我不知道必须检索的属性名称。请帮帮我!

EN

回答 4

Stack Overflow用户

发布于 2015-10-16 09:55:59

要从社交登录中获取头像,你需要在你的应用程序中创建一个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 the 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 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()
票数 13
EN

Stack Overflow用户

发布于 2015-04-11 17:46:22

这是我用来为Facebook保存图片的方法:

代码语言:javascript
复制
def save_profile_picture(backend, user, response, details,
                         is_new=False,*args,**kwargs):

    if backend.__class__.__name__ == 'FacebookOAuth2':
        up = UserProperties.objects.get_or_create(user=user) #RETURNS TUPLE (instance, created(boolean))
        if not up[0].photo:
            url = 'http://graph.facebook.com/{0}/picture'.format(response['id'])
            response = urllib.request.urlopen(url)
            io = BytesIO(response.read())
            up[0].photo.save('profile_pic_{}.jpg'.format(user.pk), File(io))
            up[0].save() 

将此函数保存到一个文件中,例如pipelines.py,然后在您的设置中将该函数添加到您的SOCIAL_AUTH_PIPELINE中。

代码语言: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.social_auth.associate_by_email', 
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'projects.pipeline.save_profile_picture', #save facebook profile image,
)

对于Facebook,您需要创建自己的Facebook应用程序。您只能检索已授予您权限的用户的信息和图片。同样的规则或多或少也适用于谷歌。有关更多详细信息,请阅读他们的API文档。

票数 6
EN

Stack Overflow用户

发布于 2020-12-23 08:44:18

上面的答案可能不起作用(对我不起作用),因为如果没有accesstoken,facebook个人资料URL就不再起作用。下面的答案对我很有效。

代码语言:javascript
复制
def save_profile(backend, user, response, is_new=False, *args, **kwargs):
    if is_new and backend.name == "facebook":
        # The main part is how to get the profile picture URL and then do what you need to do
        Profile.objects.filter(owner=user).update(
            imageUrl='https://graph.facebook.com/{0}/picture/?type=large&access_token={1}'.format(response['id'],
                                                                                                  response[
                                                                                                      'access_token']))
    elif backend.name == 'google-oauth2':
        if is_new and response.get('picture'):
            Profile.objects.filter(owner=user).update(imageUrl=response['picture'])

添加到setting.py中的管道中,

代码语言:javascript
复制
SOCIAL_AUTH_PIPELINE+ = ('<full_path>.save_profile')
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29575713

复制
相关文章

相似问题

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