【问题标题】:Using django with postman {"detail":"CSRF Failed: CSRF token missing or incorrect."}将 Django 与邮递员一起使用 {"detail":"CSRF 失败:CSRF 令牌丢失或不正确。"}
【发布时间】:2017-01-11 22:19:27
【问题描述】:

我正在使用邮递员检查来自我的 django-rest-framework 的 json 响应。

当我第一次尝试通过 POST 方法将 ID、电子邮件、密码发布到 AWS(亚马逊网络服务)上的 django 时,它运行良好。它返回如下:

  {
    "key": "99def123123123123d88e15771e3a8b43e71f"
}

但是在第一次尝试之后,换句话说,从第二次尝试它返回了

{"detail":"CSRF Failed: CSRF token missing or incorrect."}

(另外编辑+)我的腻子终端说"POST /rest-auth/login/ HTTP/1.1" 403 58

我看到了http://kechengpuzi.com/q/s31108075,但这不适合我的情况。

来自http://django-rest-framework.narkive.com/sCyJk3hM/authentication-ordering-token-vs-session,我找不到使用邮递员的解决方案

  1. 如何正确使用邮递员?

  2. 或者您能推荐其他使用的工具吗?

我正在使用retrofit2 制作android 应用程序所以我需要工具来检查POST、GET 方法和响应。

【问题讨论】:

  • 你的意思是你在两个请求之间得到不同的结果而不做任何改变?
  • 是的,我在第一次尝试和其他尝试中使用 POST 方法发布{ "username": "thesamething", "email": "thesamething", "password": "thesamething" }。当我在从 DRF(实际上是 django-rest-auth)复制的给定 DRF html 页面上以相同的方式使用 POST 方法时,没有发生此错误。但在邮递员身上,它发生了。
  • 您是否在请求中设置了 carf 令牌?
  • 我把标题 key : e0af91707f0434a1a2a7581dd3f4f48d3bdad717Authorization : e0af91707f0434a1a2a7581dd3f4f48d3bdad717Authorization : "key": "99def123123123123d88e15771e3a8b43e71f" 但它不起作用。正如你所说,我认为我使用标题是错误的。放置授权密钥的正确方法是什么?在哪里可以查到?
  • 尝试使用收到的 CSRF 令牌在 Postman 中设置 X-CSRFToken 标头(参见stackoverflow.com/questions/26639169/…

标签: android django django-rest-framework postman


【解决方案1】:

您的 api 需要 CSRF 令牌,您必须将 CSRF 令牌添加到请求(和邮递员):

data: { csrfmiddlewaretoken: csrf_token, "username": "thesamething", "email": "thesamething", "password": "thesamething" }

您可以从您的表单输入字段中获取 CSRF 令牌(如果您使用 django 内置表单 api,您会发现一个隐藏字段)或者如果您使用 Ajax,您可以查看Cross Site Request Forgery protection。使用您的授权密钥,您的密钥用于识别您的身份,CSRF 令牌用于确保此请求是从您的服务器发送的。

【讨论】:

  • 我必须在邮递员的正文中添加data: { csrfmiddlewaretoken: csrf_token, "username": "thesamething", "email": "thesamething", "password": "thesamething" } 吗?不更改标题中的任何内容?
  • 尝试使用收到的 CSRF 令牌在 Postman 中设置 X-CSRFToken 标头(参见stackoverflow.com/a/26639895/8133649
【解决方案2】:

如果在 DRF 中使用基于令牌的身份验证,请不要忘记在 settings.py 中进行设置。否则你会得到一个 CSRF 错误

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
    ]
}

【讨论】:

    【解决方案3】:

    我在使用 Postman 时遇到了同样的问题。第一次获得令牌后,我被要求在每个请求中包含一个 CSRF,所以我意识到我启用了 Session 和 Token 身份验证方法,所以我注释掉了 SessionAuthentication 行(当然,你也可以删除它)

    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication',
        # 'rest_framework.authentication.SessionAuthentication',
    ]
    

    之后,我可以只使用我的凭据而不包括任何 CSRF 代码来请求令牌:

    我认为激活这两个身份验证类的事实导致 Django 以某种方式混淆了。

    【讨论】:

    • 谢谢。可能是一个边缘案例,但我遇到了这个问题,我仍然从应用程序中删除了 JWT 令牌身份验证,但无法找出问题所在。
    【解决方案4】:

    我将请求方法从 post 更改为 patch,我可以登录

    【讨论】:

      【解决方案5】:

      您可以在您的 json 数据中使用 csrfmiddlewaretoken: csrf_token,其中 csrf_token 是一个有效的令牌,但在包含它的情况下您无法提供正确的令牌,评论或删除 SessionAuthentication,如下所示。

      'DEFAULT_AUTHENTICATION_CLASSES': [
          'rest_framework.authentication.TokenAuthentication',
          # 'rest_framework.authentication.SessionAuthentication',
      ]
      

      【讨论】:

        【解决方案6】:

        对我来说,解决方案是在 Postman 中添加 X-CSRFToken 标头(从浏览器中的初始登录响应中获得)

        https://stackoverflow.com/a/26639895/8133649

        【讨论】:

          【解决方案7】:

          在settings.py文件中

          INSTALLED_APPS = [
          ...
          ...
          ...
          ...
          'rest_framework.authtoken',
          ...
          ]
          
          REST_FRAMEWORK = {
              'DEFAULT_AUTHENTICATION_CLASSES': (
                  'rest_framework.authentication.TokenAuthentication',
              ),
          }
          
          

          在项目 urls.py 中

          from rest_framework.authtoken import views
          
          urlpatterns = [
              ....
              path('api-token-auth/',views.obtain_auth_token,name='api-token-auth')
          
          ]
          
          

          打开终端

          $ pip3 install httpie
          $ python3 manage.py createsuperuser # if not created
          $ http POST http://localhost:8000/api-token-auth/ username="username" password = "password"   # You will get token key (Just copy it) ex:a243re43fdeg7r4rfgedwe89320
          
          

          您的令牌密钥也将自动保存在您的数据库中

          转到邮递员标题(如示例) 例如:screenshot from postman ,where and how to paste accessed toke 然后插入你的令牌密钥。

          reference to get token key from this video

          【讨论】:

            猜你喜欢
            • 2014-02-26
            • 2021-10-01
            • 1970-01-01
            • 2021-06-04
            • 2014-12-25
            • 2020-09-19
            • 2021-07-14
            • 2021-11-13
            • 2012-04-20
            相关资源
            最近更新 更多