有没有办法使用python社交网站获取谷歌个人资料图片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“吗?第二,我应该使用什么url来保存配置文件的化身?是这条路吗?
发布于 2014-09-09 12:53:46
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()发布于 2015-10-16 01:52:15
要从社交登录获得化身,您需要在应用程序中创建一个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()发布于 2014-09-09 03:41:28
我不熟悉Python,但是看起来您需要GooglePlusAuth。
要获得图像url,您必须向people.get API方法发出HTTP请求,userId设置为me,并作为用户进行身份验证。响应包括一个image.url值。
https://stackoverflow.com/questions/25734611
复制相似问题