使用 API 键从 Flask RESTful API 获取

GET from Flask RESTful API with API Key

我是这个领域的新手,我正在努力从我的烧瓶服务器执行成功的 GET。我的服务器 运行ning 在 docker 上 Ubuntu 18.04 服务器虚拟机上,我的主机 OS 是 Ubuntu 19.10.

我不知道你需要哪些信息来帮助我。我得到了一个 YAML 文件,我从中生成了 API。首先是我的 YAML 文件中的几行,我从以下位置生成了所有内容:

servers:
    - url: /v1
paths:
  /supported_types:
    get:
      operationId: getSupportedTypes
      summary: Retrieve supported types
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  username:
                    type: boolean
                  email_address:
                    type: boolean

我的安全部分:

security:
  - APIKey: []
components:
  securitySchemes:
    APIKey:
      type: apiKey
      in: header
      name: Authorization

在我的代码中我没有做太多改动所以我的 authentication_controller.py 看起来像这样:

def check_APIKey(api_key, required_scopes):
    return {'test_key': 'test_value'}

在我的 default_controller.py 中有这个方法,我认为它是指定的 GET 方法:

def get_supported_types():  # noqa: E501
    """Retrieve supported types
     # noqa: E501
    :rtype: InlineResponse200
    """
    notes = {username:true, email_address:true}
    return notes

所以当我 运行 curl -H 'X-Api-Key: DEMO_KEY' --trace-ascii /tmp/dump.txt http://127.0.0.1:3080/v1/supported_types 我通过控制台得到这个输出:

{
  "detail": "No authorization token provided",
  "status": 401,
  "title": "Unauthorized",
  "type": "about:blank"
}

在 dump.txt 中它说:

== Info:   Trying 127.0.0.1:3080...
== Info: TCP_NODELAY set
== Info: Connected to 127.0.0.1 (127.0.0.1) port 3080 (#0)
=> Send header, 120 bytes (0x78)
0000: GET /v1/supported_types HTTP/1.1
0025: Host: 127.0.0.1:3080
003b: User-Agent: curl/7.65.3
0054: Accept: */*
0061: X-Api-Key: DEMO_KEY
0076:
== Info: Mark bundle as not supporting multiuse
== Info: HTTP 1.0, assume close after body
<= Recv header, 27 bytes (0x1b)
0000: HTTP/1.0 401 UNAUTHORIZED
<= Recv header, 40 bytes (0x28)
0000: Content-Type: application/problem+json
<= Recv header, 21 bytes (0x15)
0000: Content-Length: 119
<= Recv header, 39 bytes (0x27)
0000: Server: Werkzeug/0.16.0 Python/3.6.10
<= Recv header, 37 bytes (0x25)
0000: Date: Sun, 16 Feb 2020 14:17:22 GMT
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 119 bytes (0x77)
0000: {.  "detail": "No authorization token provided",.  "status": 401
0040: ,.  "title": "Unauthorized",.  "type": "about:blank".}.
== Info: Closing connection 0

我很确定我遗漏了一些非常垃圾的东西,但也许有人可以告诉我它是什么(也许可以解释我可能误解了什么。

我弄清楚问题出在哪里:就像评论中告诉我的那样,我必须调整 header

I think you send the DEMO_KEY in the Authorization header instead of the X-Api-Key header – mtshaikh

我把curl -H 'X-Api-Key: DEMO_KEY'改成了curl -H 'Authorization: DEMO_KEY'

然后我在

中发现了我的代码的另一个错误
def get_supported_types():  # noqa: E501
    """Retrieve supported types
     # noqa: E501
    :rtype: InlineResponse200
    """
    notes = {username:true, email_address:true}
    return notes

我改变的地方

notes = {username:true, email_address:true}
        return notes

变成return "test"所以它会变得可解释