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