Python Social Auth Gets Google Avatar

Is there a way to get the url of a google profile picture using python social auth?

So this is what I do for facebook and twitter, add a pipeline with this code:

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 = "???" 

First, I don’t know what the backend name is, is it “GoogleOAuth2”? Secondly, what is the URL that I have to use to save the profile avatar ?. Is it so?

+5
source share
4 answers
 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
source

To get avatars from social input, you need to create a pipe.py file in your application and add these lines to 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. ) 

and then add this content to the pipe.py file

 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() 
+1
source

I am not familiar with Python Social Auth, but it looks like you want GooglePlusAuth .

In order to get the image URL, you will need to make an HTTP request to the people.get API method with userId set to me and authenticated as a user. The answer includes the value of image.url .

0
source

I use the approach suggested by vero4ka.

import pdb; pdb.set_trace() python debugging ( import pdb; pdb.set_trace() ) around the line ext = url.split('.')[-1] , I notice that the split output does not completely separate the file extension:

 (Pdb) url = response['image'].get('url').split('.') (Pdb) url [u'https://lh4', u'googleusercontent', u'com/redacted/redacted/photo', u'jpg?sz=50'] 

Do I need to separate the character as well ? . Nothing wrong.

Not sure why you need to split and then recompile the extension this way?

0
source

Source: https://habr.com/ru/post/1202112/


All Articles