2016-04-28 19 views
10

Sto usando il fantastico Python social auth con Django.
Tuttavia, al momento, ogni volta che viene richiamato il processo, viene creato un nuovo utente. Ho solo bisogno dei token (access_token e refresh_token) dal processo. Come può essere realizzato? Attraverso una sorta di gasdotto?Utilizza l'autenticazione sociale Python per ottenere solo token

Questo è il mio codice pipeline.py al momento (abbreviato):

def get_token(backend, user, response, *args, **kwargs): 

    # get token from the oauth2 flow 
    social = user.social_auth.get(provider='google-oauth2') 
    access_token = social.extra_data['access_token'] 
    refresh_token = social.extra_data.get('refresh_token') 

e il corrispondente file di settings.py:

# set django session 
SESSION_EXPIRE_AT_BROWSER_CLOSE = True 

# psa settings 
SOCIAL_AUTH_URL_NAMESPACE = 'social' 

# see http://psa.matiasaguirre.net/docs/configuration/settings.html 
SOCIAL_AUTH_UUID_LENGTH = 32 

AUTHENTICATION_BACKENDS = (
    #'social.backends.facebook.FacebookOAuth2', 
    'social.backends.google.GoogleOAuth2', 
    #'social.backends.twitter.TwitterOAuth', 
    'django.contrib.auth.backends.ModelBackend', 
) 

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', 
    'youtube.pipeline.get_token', 
) 
+0

* "... ogni volta che viene richiamato il processo, viene creato un nuovo utente ..." * - quale processo? – xyres

+0

@xyres: il processo di autenticazione di un utente verso Google+ o Facebook o Twitter. Inizia con un collegamento nel modello. – Jan

risposta

4

Sì, è tutto in cantiere. Se dai un'occhiata a quello che hai già, vedrai anche il passo social.pipeline.user.create_user.

From the documentation:

# Create a user account if we haven't found one yet. 
'social.pipeline.user.create_user', 

(source for that function)

Sostituire questo (e tutti i seguenti passaggi se non hai bisogno di loro) con qualunque è che si sta cercando di raggiungere.

def get_token(backend, uid, *args, **kwargs): 
    # get token from the oauth2 flow 
    provider = backend.name 
    social = backend.strategy.storage.user.get_social_auth(provider, uid) 
    access_token = social.extra_data['access_token'] 
    refresh_token = social.extra_data.get('refresh_token') 

Il metodo modificato dovrebbe funzionare anche se nessun utente esiste/viene creato.

+0

Vado a provare questo. – Jan

+0

Ricevo un errore che il 'uid' non è definito - come ottengo questo? – Jan

+0

Ah, mi spiace, sembra che ['uid' sia uno degli argomenti della funzione] (https://github.com/MSOpenTech/python-social-auth/blob/master/social/pipeline/social_auth.py#L18) (creato dal passaggio "social.pipeline.social_auth.social_uid'). Post aggiornato – Anonymous