有没有办法使用python social auth获取Google个人资料图片网址?
所以,这就是我为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 = "???"
首先,我不知道后端的名称是什么,是#34; GoogleOAuth2"? 其次,我应该使用什么网址来保存个人资料的头像?是这样的吗?
答案 0 :(得分: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()
答案 1 :(得分: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()
答案 2 :(得分:0)
我不熟悉Python Social Auth,但看起来你想要GooglePlusAuth。
要获取图片网址,您必须向people.get API method发送HTTP请求,userId
设置为me
并作为用户进行身份验证。回复包含image.url
值。
答案 3 :(得分:0)
我正在使用vero4ka建议的方法。
在import pdb; pdb.set_trace()
行附近放入python调试(ext = url.split('.')[-1]
),我注意到分割输出没有完全分离文件扩展名viz:
(Pdb) url = response['image'].get('url').split('.')
(Pdb) url
[u'https://lh4', u'googleusercontent', u'com/redacted/redacted/photo', u'jpg?sz=50']
我也需要拆分?
符号。没什么大不了的。
不确定为什么会分裂然后以这种方式重新组合扩展?