python - 使用 API key 从 Flask RESTful API 获取

标签 python api flask api-key

我对这个领域还很陌生,我正在努力从我的 Flask 服务器执行成功的 GET。我的服务器在 Ubuntu 18.04 服务器虚拟机上的 docker 中运行,我的主机操作系统是 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

我很确定我错过了一些真正转储的东西,但也许有人可以告诉我它是什么(也许可以解释我可能误解的内容。

最佳答案

我找出了问题所在:就像评论中告诉我的那样,我必须调整标题

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

进入返回“test”,这样它将变得可解释

关于python - 使用 API key 从 Flask RESTful API 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60249436/

相关文章:

api - 从 Etsy 商店获取所有列表图像

java - Swagger Spring API - xmlModelPlugin 错误

python - 找到位于球形区域内的所有点

python - Django manage.py runserver 无法响应

Python basemap : Error using shadedrelief, bluemarble 或 etopo(假经度格式?)

python - 如何从 celery 后端存储中检索元

python - 如何在 Flask/Jinja 模板中使用与过滤器不同的管道?

python - 使用 GNU screen 时的权限问题

reactjs - ReactJS 和 .NET Core API 中的 windows 授权

python - 除了 GET、PUT、POST、DELETE 之外的 Flask-RESTful 自定义路由