2015-12-11 128 views
1

更新:從Nginx配置中刪除了set_header令牌。意識到這並不能解決問題。我覺得問題在於Django沒有得到正確的頭文件,並且他們在某處丟失了。DRF總是返回「未提供身份驗證憑據」

我正在嘗試爲祕密聖誕老人項目創建一個REST api。我已經設置好了,用戶必須先進行身份驗證才能進行某些調用。這在我的本地機器上工作正常,但在任何地方託管時似乎不起作用。

我已經在Heroku和Ubuntu服務器上試過了。不過,我寧願讓它在Ubuntu服務器上工作。我使用gunicorn和nginx來爲應用程序服務,但是我在所有需要身份驗證的調用中都收到「未提供身份驗證憑據」。我正在使用TokenAuthentication,並將令牌傳遞給帶有前綴Token的Authorization頭文件。

任何幫助,非常感謝。

settings.py

REST_FRAMEWORK = { 
    'PAGINATE_BY': 30, 
    'PAGINATE_BY_PARAM': 'per_page', 
    'MAX_PAGINATE_BY': 1000, 
    "DATETIME_FORMAT": "%Y-%m-%dT%H:%M:%S%z", 
    'DEFAULT_RENDERER_CLASSES': (
     'rest_framework.renderers.JSONRenderer', 
     'rest_framework.renderers.BrowsableAPIRenderer', 
    ), 
    'DEFAULT_PERMISSION_CLASSES': [ 
     'rest_framework.permissions.IsAuthenticated', 
    ], 
    'DEFAULT_AUTHENTICATION_CLASSES': (
     'rest_framework.authentication.TokenAuthentication', 
    ), 
} 

views.py

class RoomViewSet(mixins.RetrieveModelMixin, 
       viewsets.GenericViewSet): 
""" 
Creates, Updates, and retrives Rooms 
""" 

queryset = Room.objects.all() 
serializer_class = RoomSerializer 
permission_classes = (IsAuthenticated,) 
lookup_field = 'slug' 

gunicorn.conf

description "Gunicorn application server handling myproject" 

start on runlevel [2345] 
stop on runlevel [!2345] 

respawn 
setuid dannywilson 
setgid www-data 
chdir /storage/sites/secret_santa/ 

exec santa/bin/gunicorn --pythonpath="$PWD/secret_santa" --bind=unix:"$PWD/secret_santa/gunicorn.sock" wsgi:application 

兩nginx的配置

upstream test_server { 
    server unix:/storage/sites/secret_santa/secret_santa/gunicorn.sock; 
} 

server { 
    listen 80; 
    server_name webaddress; 

    access_log /storage/sites/_logs/secret_santa_api/nginx-access.log; 
    error_log /storage/sites/_logs/secret_santa_api/nginx-error.log; 

    location/{ 

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_ignore_headers Cache-Control Expires Pragma; 

     if (!-f $request_filename) { 
      proxy_pass http://test_server; 
      break; 
     } 

    } 
} 
+0

你發送了什麼頭文件actly? – utkbansal

+0

只需使用令牌 – danwilson

+0

即可獲得授權標題您是如何命名標題的? – utkbansal

回答

1

兩件事情在這裏給我的印象:

  • 「proxy_set_header令牌$ http_token;」但我不認爲$ http_token是定義的。
  • 使用「Token」而不是「Authorization」作爲標題。 Documentation提到「要使客戶端進行身份驗證,令牌密鑰應包含在授權HTTP頭中。」
0

,因爲以下配置的:

REST_FRAMEWORK = { 
........ 
'DEFAULT_PERMISSION_CLASSES': [ 
    'rest_framework.permissions.IsAuthenticated',], 
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',), 
} 

如果您不需要身份驗證刪除這兩配置,否則,如果你想使用TokenAuthentication需要添加rest_framework.authtoken到安裝的應用程序:

INSTALLED_APPS = (
... 
'rest_framework.authtoken' 
) 

然後按照TokenAuthentication的說明

+0

我已在我安裝的應用程序中獲得了rest_framework.authtoken。我的問題是,當它不在我的本地測試服務器上時,我無法驗證我的任何API調用。 – danwilson

+0

因此,如果它對本地服務器而不是遠程服務器正常工作,您可能會發現這裏的解決方案很有幫助[請點擊此處](http:// stackoverflow。COM /問題/ 34206779/CSRF-cookie的未設置時張貼請求與 - angularjs-Django的後端/ 34213423#34213423) – DhiaTN

相關問題