Search code examples
pythonapiflaskapi-key

GET from Flask RESTful API with API Key


I'm pretty new to this field and I'm struggling to perform a successful GET from my flask server. I have my server running in docker on a Ubuntu 18.04 Server VM and my host OS is Ubuntu 19.10.

I don't know which information you need to help me. I got a YAML file which I generated my API from. First a few lines from my YAML file, I generated everything from:

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

And my security section:

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

In my code I didn't change much so my authentication_controller.py looks like this:

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

In my default_controller.py there is this method which I assume to be the specified GET method:

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

So when I run curl -H 'X-Api-Key: DEMO_KEY' --trace-ascii /tmp/dump.txt http://127.0.0.1:3080/v1/supported_types I get this output via console:

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

And in the dump.txt it says:

== 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'm pretty sure I'm missing something really dump, but maybe someone can tell me what it is (and maybe explain what I may be misunderstanding.


Solution

  • I figured out what the problem was: like I was told in the comments I had to adjust the header

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

    I changed curl -H 'X-Api-Key: DEMO_KEY' to curl -H 'Authorization: DEMO_KEY'.

    Then I found another error with my code in

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

    where I changed

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

    into return "test" so it will become interpretable