2014-10-30 14 views
6

Ho un'API REST e devo autenticare gli utenti tramite l'API di accesso di Facebook. Il token di accesso deve essere ottenuto nell'app mobile (credo) e quindi inviato al server. Quindi ho trovato del codice nel vecchio tutorial e non riesco a farlo funzionare. Ecco il codice:Come autenticarsi con il token di accesso nel codice utilizzando python-social-auth

from social.apps.django_app.utils import strategy 
from django.contrib.auth import login 
from django.views.decorators.csrf import csrf_exempt 
from rest_framework.decorators import api_view, permission_classes 
from rest_framework import permissions, status 
from django.http import HttpResponse as Response 


@strategy() 
def auth_by_token(request, backend): 
    user=request.user 
    user = backend.do_auth(
     access_token=request.DATA.get('access_token'), 
     user=user.is_authenticated() and user or None 
     ) 
    if user and user.is_active: 
     return user 
    else: 
     return None 



@csrf_exempt 
@api_view(['POST']) 
@permission_classes((permissions.AllowAny,)) 
def social_register(request): 
    auth_token = request.DATA.get('access_token', None) 
    backend = request.DATA.get('backend', None) 
    if auth_token and backend: 
     try: 
      user = auth_by_token(request, backend) 
     except Exception, err: 
      return Response(str(err), status=400) 
     if user: 
      login(request, user) 
      return Response("User logged in", status=status.HTTP_200_OK) 
     else: 
      return Response("Bad Credentials", status=403) 
    else: 
     return Response("Bad request", status=400) 

Quando cerco di inviare richiesta POST con i parametri, ottengo questo errore:

'unicode' object has no attribute 'do_auth' 

ho trovato un esempio in official documentation, utilizza @psa('social:complete') decoratore:

from django.contrib.auth import login 

from social.apps.django_app.utils import psa 

# Define an URL entry to point to this view, call it passing the 
# access_token parameter like ?access_token=<token>. The URL entry must 
# contain the backend, like this: 
# 
# url(r'^register-by-token/(?P<backend>[^/]+)/$', 
#  'register_by_access_token') 

@psa('social:complete') 
def register_by_access_token(request, backend): 
    # This view expects an access_token GET parameter, if it's needed, 
    # request.backend and request.strategy will be loaded with the current 
    # backend and strategy. 
    token = request.GET.get('access_token') 
    user = backend.do_auth(request.GET.get('access_token')) 
    if user: 
     login(request, user) 
     return 'OK' 
    else: 
     return 'ERROR' 

Ma cosa succede se ho bisogno di passare il nome del backend nel corpo della richiesta?

+1

Dovrebbe essere '' request.backend.do_auth (request.GET.get ('access_token')) '', Ho aggiornato i documenti con lo snippet di destra. – omab

+0

Potresti postarlo come risposta? –

risposta

3

Deve essere request.backend.do_auth(request.GET.get('access_token')).

Ho aggiornato i documenti con lo snippet di destra http://psa.matiasaguirre.net/docs/use_cases.html#signup-by-oauth-access-token.

+0

Avevo bisogno di aggiungere request = request agli argomenti do_auth - senza che questo la pipeline social_auth.backends.pipeline.misc.save_status_to_session stava causando al flusso di autorizzazione di non restituire un utente. @omab se pensi sia valido, ti preghiamo di aggiornare i documenti - mi ci sono voluti due giorni per eseguire il debug di questo! –

Problemi correlati