2017-05-31 71 views
0

我是角JS和Django的初學者。我在製作Facebook身份驗證應用程序時遵循此特定教程。使用角JS和Django的Facebook認證

http://cbdev.blogspot.in/2014/02/facebook-login-with-angularjs-django.html

我跟着教程完全相同。當我啓動服務器時,出現錯誤。

NameError at/

name 'strategy' is not defined 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/ 
Django Version:  1.3.1 
Exception Type:  NameError 
Exception Value:  

name 'strategy' is not defined 

Exception Location:  /root/Documents/django/clueless/clueless_engine/../clueless_engine/views.py in <module>, line 1 
Python Executable: /root/Documents/django/clueless/bin/python 
Python Version:  2.7.13 
Python Path:  

['/root/Documents/django/clueless/clueless_engine', 
'/root/Documents/django/clueless/lib/python2.7', 
'/root/Documents/django/clueless/lib/python2.7/plat-x86_64-linux-gnu', 
'/root/Documents/django/clueless/lib/python2.7/lib-tk', 
'/root/Documents/django/clueless/lib/python2.7/lib-old', 
'/root/Documents/django/clueless/lib/python2.7/lib-dynload', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/root/Documents/django/clueless/local/lib/python2.7/site-packages', 
'/root/Documents/django/clueless/lib/python2.7/site-packages'] 

Server time: Thu, 1 Jun 2017 07:30:14 +0530 

我views.py文件

@strategy() 
def auth_by_token(request, backend): 
    backend = request.strategy.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# Return anything that makes sense here 
    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: 
      strategy = load_strategy(request=request, backend=backend) 
      _do_login(strategy, 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) 

回答

0

我已經使用python scocial權威性創造社會驗證認證。你可以檢查: https://github.com/ranvijay-sachan/django-rest-login-and-social_auth/tree/master/profiles

POST:http://localhost:8000/api/v1/login/2/

Content-Type : application/json 

{ "accessToken": "alert token" } 
+0

謝謝。但是你能找到導致這個錯誤的原因嗎? –

+0

我認爲你需要導入模塊,如: 從social.apps.django_app.utils導入load_strategy 策略= load_strategy(要求) –

+0

我無法相信我錯過了! –

0

的問題是,我忘了導入模塊。

from social.apps.django_app.utils import load_strategy strategy = load_strategy(request)