Curl 使用令牌身份验证请求后重定向到登录页面

Curl 使用令牌身份验证请求后重定向到登录页面,curl,django-rest-framework,token,Curl,Django Rest Framework,Token,我有一个django应用程序,其中我们有用于UI的基本django视图和模板。现在我们想用react替换前端。对于身份验证,我们使用TokenAuthentication。因此,如果我理解正确,它应该是这样工作的: curl -X POST "http://0.0.0.0:8001/api/v1/api-token-auth/" -H "accept: application/json" -H "Content-Type: applicati

我有一个django应用程序,其中我们有用于UI的基本django视图和模板。现在我们想用react替换前端。对于身份验证,我们使用
TokenAuthentication
。因此,如果我理解正确,它应该是这样工作的:

curl -X POST "http://0.0.0.0:8001/api/v1/api-token-auth/" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"username\": \"user-name\",  \"password\": \"S3cure-P4ssw0rd\"}"
  • 用户名和密码发布到API以获取令牌
  • 向您的API发出请求,并将您的令牌添加到标头,如
    授权:令牌31271c25207ef084ca6e1c0af65a08d0c8f0897a

  • 为了获得令牌,我将其添加到我的
    url.py

    path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
    
    urlpatterns = [
        path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
        path(r"accounts/login/", auth_views.LoginView.as_view()),
        path(r"api/v1/test/", test.index_api, name="index_api"),
        ...
        path("logout/", LogoutView.as_view(), name="logout"),
    ]
    
    REST_FRAMEWORK = {
        "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.TokenAuthentication",
            "rest_framework.authentication.SessionAuthentication",
        ),
        "TEST_REQUEST_DEFAULT_FORMAT": "json",
    }
    
    在发布用户名和密码后返回令牌,如下所示:

    curl -X POST "http://0.0.0.0:8001/api/v1/api-token-auth/" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"username\": \"user-name\",  \"password\": \"S3cure-P4ssw0rd\"}"
    
    这将返回:

    {“令牌”:“31271c25207ef084ca6e1c0af65a08d0c8f0897a”}

    这个很好用

    现在我想
    从我的REST-API中获取一些东西,但是当我尝试它时,就像:

    curl -X GET "http://0.0.0.0:8001/api/v1/test/" -H  "accept: application/json" -H  "Authorization: Token 31271c25207ef084ca6e1c0af65a08d0c8f0897a"
    
    它给出了一个
    302
    ,并重定向到
    /accounts/login/?next=/api/v1/test/
    ,我不知道为什么


    下面是一些代码:

    url.py

    path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
    
    urlpatterns = [
        path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
        path(r"accounts/login/", auth_views.LoginView.as_view()),
        path(r"api/v1/test/", test.index_api, name="index_api"),
        ...
        path("logout/", LogoutView.as_view(), name="logout"),
    ]
    
    REST_FRAMEWORK = {
        "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.TokenAuthentication",
            "rest_framework.authentication.SessionAuthentication",
        ),
        "TEST_REQUEST_DEFAULT_FORMAT": "json",
    }
    
    settings.py

    path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
    
    urlpatterns = [
        path(r"api/v1/api-token-auth/", views.obtain_auth_token, name="api_token_auth"),
        path(r"accounts/login/", auth_views.LoginView.as_view()),
        path(r"api/v1/test/", test.index_api, name="index_api"),
        ...
        path("logout/", LogoutView.as_view(), name="logout"),
    ]
    
    REST_FRAMEWORK = {
        "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"],
        "DEFAULT_AUTHENTICATION_CLASSES": (
            "rest_framework.authentication.TokenAuthentication",
            "rest_framework.authentication.SessionAuthentication",
        ),
        "TEST_REQUEST_DEFAULT_FORMAT": "json",
    }
    

    如果需要更多信息,请告诉我

    谢谢大家!

    好吧,我想出来了

    我的视图中有装饰者
    @login\u required
    @staff\u member\u required
    ,它们来自django,而不是rest\u框架。我不得不使用rest_框架权限类而不是Django的decorator

    本文的解释和更多细节: