0

我需要一个非常安全的云功能,所以我试图把它放在 API 网关后面。当我直接在标头中传递 Bearer 令牌时,该函数工作正常:

https://us-central1-<my-project>.cloudfunctions.net/<my-hello-function>

但是我想允许它通过 API 网关与 API 令牌一起使用(然后做一些比说“你好”更有用的事情):

https://my-gateway-xxxxxxxx.uc.gateway.dev/v1/stats&key=<my-API-token>

当我尝试调用它时,我得到:

{ "code": 404, "message": "路径不匹配任何需求 URI 模板。" }

我的 API 网关配置文件:

swagger: "2.0"
info:
  title: my-gateway
  version: "1.0.0"
basePath: "/v1"
schemes:
 - "https"
produces:
 - application/json
paths:
  /stats:
    get:
      tags:
      - "stats"
      summary: "get service stats"
      description: "Returns statistics"
      operationId: "hello_world"
      #produces:
      #- "application/json"
      parameters:
      - name: "since"
        in: "header"
        description: "Date to retrieve information"
        required: false
        type: "string"
        format: "date"
      x-google-backend:
          address: https://us-central1-<my-project>.cloudfunctions.net/<my-hello-function>
          path_translation: CONSTANT_ADDRESS
          protocol: h2
      responses:
        "200":
          description: "successful operation"
          schema:
            $ref: "#"
        "400":
          description: "Invalid datetime supplied"
        "404":
          description: "Unknown path"
      security:
      - api_key: []
securityDefinitions:
  api_key:
    type: "apiKey"
    name: "api_key"
    in: "query"
definitions:
  ApiResponse:
    type: "object"
    properties:
      code:
        type: "integer"
        format: "int32"
      type:
        type: "string"
      message:
        type: "string"

少了什么东西?我究竟做错了什么?

4

1 回答 1

2

我测试了您的文件并查看了您的 HTTP 调用。我注意到在您的安全定义中,您将 API 密钥命名为,api_key但在您的 URL 中,您使用的是参数key,此外,没有必要设置path_translation: CONSTANT_ADDRESS ,因为这是默认指令

此外,您可以检查您的网关是否使用最新配置。

这是我使用的配置并按预期工作(我将其更改apikeykey并删除path_translation

swagger: "2.0"
info:
  title: my-gateway
  version: "1.0.0"
basePath: "/v1"
schemes:
 - "https"
produces:
 - application/json
paths:
  /stats:
    get:
      tags:
      - "stats"
      summary: "get service stats"
      description: "Returns statistics"
      operationId: "hello_world"
      parameters:
      - name: "since"
        in: "header"
        description: "Date to retrieve information"
        required: false
        type: "string"
        format: "date"
      x-google-backend:
          address: https://us-central1-[myproject].cloudfunctions.net/[functionname]
          protocol: h2
      responses:
        "200":
          description: "successful operation"
          schema:
            $ref: "#"
        "400":
          description: "Invalid datetime supplied"
        "404":
          description: "Unknown path"
      security:
      - api_key: []
securityDefinitions:
  api_key:
    type: "apiKey"
    name: "key"
    in: "query"
definitions:
  ApiResponse:
    type: "object"
    properties:
      code:
        type: "integer"
        format: "int32"
      type:
        type: "string"
      message:
        type: "string"
于 2020-12-09T22:13:34.727 回答