【问题标题】:JSON:API with django-rest-framework-json-api and JWTJSON:带有 django-rest-framework-json-api 和 JWT 的 API
【发布时间】:2019-04-26 19:09:27
【问题描述】:

我认为在我的新项目中使用标准 JSON:API 可能是个好主意。不幸的是,我立即无法让 JWT 身份验证正常工作。 我的设置:

  • 姜戈
  • Django REST 框架
  • REST 框架 JWT 身份验证
  • Django REST 框架 JSON API

如果我的身份验证路径有 OPTIONS:

{
    "data": {
        "name": "Obtain Json Web Token",
        "description": "API View that receives a POST with a user's username and password.\n\nReturns a JSON Web Token that can be used for authenticated requests.",
        "renders": [
            "application/vnd.api+json",
            "text/html"
        ],
        "parses": [
            "application/vnd.api+json",
            "application/x-www-form-urlencoded",
            "multipart/form-data"
        ],
        "allowed_methods": [
            "POST",
            "OPTIONS"
        ],
        "actions": {
            "POST": {
                "username": {
                    "type": "String",
                    "required": true,
                    "read_only": false,
                    "write_only": false,
                    "label": "Username"
                },
                "password": {
                    "type": "String",
                    "required": true,
                    "read_only": false,
                    "write_only": true,
                    "label": "Password"
                }
            }
        }
    }
}

如果我然后尝试使用 Content-Type 天真地发布:application/vnd.api+json:

{
    "data": {
        "user": "user1",
        "password": "supersecretpw"
    }
}

我收到 409 冲突响应:

{
    "errors": [
        {
            "detail": "The resource object's type (None) is not the type that constitute the collection represented by the endpoint (ObtainJSONWebToken).",
            "source": {
                "pointer": "/data"
            },
            "status": "409"
        }
    ]
}

如何正确检索令牌或正确使用上述软件包?

【问题讨论】:

    标签: django django-rest-framework jwt json-api django-rest-framework-jwt


    【解决方案1】:

    您的负载不是有效的 JSON API 文档。它必须有一个resource objectdata 键上的资源对象集合。资源对象必须具有 idtype 成员。属性应在attributes 键上表示为attributes object

    报告的错误似乎与缺少type 成员有关。因此它假定一个类型为None,它是“不是构成由端点表示的集合的类型”。最后一部分似乎特定于 Django REST Framework JSON API 实现。

    请注意,JSON API 规范与身份验证无关,因此取决于您的实施。您不必使用 JSON API 资源对象来表示凭据。 JSON API 通常不用于与身份验证相关的端点,因为已实施的身份验证标准或已建立的约定建议该端点使用另一种有效负载结构。

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 1970-01-01
      • 2020-03-22
      • 2016-12-26
      • 2017-01-04
      • 1970-01-01
      • 2018-05-17
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多