2014-09-08 24 views
5

C'è un modo per ottenere l'URL dell'immagine del profilo di Google utilizzando l'auth di social python?Python Social Auth ottiene Google avatar

Quindi, questo è quello che faccio per Facebook e Twitter, aggiungere una pipeline con questo codice:

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

In primo luogo, non so che cosa è il nome del back-end, è "GoogleOAuth2" ? In secondo luogo, qual è l'url che dovrei usare per salvare l'avatar del profilo ?. È così?

risposta

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() 
+0

Grazie vero4ka. Non volevo salvare l'immagine, solo l'url così 'url_50 = response [' image ']. Get (' url ') 'ha fatto il trucco. Spero che google non cambi il formato dell'URL in qualunque momento presto. – alejoss

0

Non ho familiarità con Python Social Auth ma sembra che tu voglia GooglePlusAuth.

Per ottenere l'URL immagine che si dovrà fare una richiesta HTTP al people.get API method con userId set per me e autenticate come l'utente. La risposta include un valore image.url.

0

sto usando l'approccio suggerito dal vero4ka.

cadere in una di debug python (import pdb; pdb.set_trace()) intorno alla linea ext = url.split('.')[-1], ho notato che l'uscita divisa non separa completamente l'estensione del file e cioè:

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

avrò bisogno di scindere il ? anche il simbolo. Nessun grosso problema.

Non sei sicuro del motivo per cui uno si divide e quindi ricombina l'estensione in questo modo?

1

Per ottenere avatar dal login sociale, è necessario creare un file pipeline.py nella vostra applicazione e aggiungere queste righe a 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. 
) 

e poi aggiungere questo contenuto al file 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() 
+0

Benvenuti in SO. Se non ti dispiace, dovresti aggiungere una spiegazione al tuo codice. O commentalo almeno. Puoi controllare [questa meta domanda sulle risposte solo in codice] (http://meta.stackexchange.com/questions/148272/is-there-any-benefit-to-allowing-code-only-answers-while-blocking- code-only-ques) – GabrielOshiro

+0

Ciao !, ho intenzione di modificare questo –

Problemi correlati