Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform Google API网关:在标头中提供API密钥_Google Cloud Platform_Google Api Gateway - Fatal编程技术网

Google cloud platform Google API网关:在标头中提供API密钥

Google cloud platform Google API网关:在标头中提供API密钥,google-cloud-platform,google-api-gateway,Google Cloud Platform,Google Api Gateway,我正在尝试将Google API网关设置为使用调用者在标头中发送的API密钥。 我的api配置yaml如下所示: 。。。 证券定义: api_密钥_标题: 类型:apiKey 姓名:key 在:标题 api\u密钥\u查询: 类型:apiKey 姓名:key in:查询 路径: /foo标题: 获取: 概要:测试foo端点 操作ID:testGet标头 x-google-backend: 地址:“ 协议:h2 路径\转换:将\路径\附加到\地址 安全: -api_密钥_头:[] 响应: 204:

我正在尝试将Google API网关设置为使用调用者在标头中发送的API密钥。
我的api配置yaml如下所示:

。。。
证券定义:
api_密钥_标题:
类型:apiKey
姓名:key
在:标题
api\u密钥\u查询:
类型:apiKey
姓名:key
in:查询
路径:
/foo标题:
获取:
概要:测试foo端点
操作ID:testGet标头
x-google-backend:
地址:“
协议:h2
路径\转换:将\路径\附加到\地址
安全:
-api_密钥_头:[]
响应:
204:
描述:成功的响应
/foo查询:
获取:
概要:测试foo端点
操作ID:testGet标头
x-google-backend:
地址:“
协议:h2
路径\转换:将\路径\附加到\地址
安全:
-api_key_查询:[]
响应:
204:
描述:成功的响应
如果没有通过header或query参数提供有效的API键,我预计这两个调用,
/foo header
/foo query
都会失败,并显示401状态

但事实上,只有
/foo query
的行为符合预期。
/foo头的请求传递到后端,即使请求头中没有提供API密钥


我是否对配置有问题,或者当请求头中提供API密钥时,是Google API网关无法正常工作?

当请求头中提供API密钥时,Google API网关似乎可以正常工作,因为Google API网关文档说明:

开发人员在云控制台的项目中生成API密钥,并将该密钥作为查询参数或请求嵌入到对API的每次调用中

paths:
  /foo-header:
    get:
      summary: Test security
      operationId: headerkey
      x-google-backend:
        address: [MY_CLOUD_FUNCTION_1]
      security:
      - api_key_header: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
  /foo-query:
    get:
      summary: Test security
      operationId: querykey
      x-google-backend:
        address: [MY_CLOUD_FUNCTION_2]
      security:
      - api_key_query: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
securityDefinitions:
  # This section configures basic authentication with an API key.
  api_key_header:
    type: "apiKey"
    name: "key"
    in: "header"
  api_key_query:
    type: "apiKey"
    name: "key"
    in: "query"
但是,我能够重现您报告的行为,因此我不认为您的配置中存在错误

为此,我一直遵循Google API网关的GCP,对其进行了轻微修改,使我的OpenAPI规范也有两条路径:一条是在查询参数中查找一个键,另一条是在请求头中查找一个键

paths:
  /foo-header:
    get:
      summary: Test security
      operationId: headerkey
      x-google-backend:
        address: [MY_CLOUD_FUNCTION_1]
      security:
      - api_key_header: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
  /foo-query:
    get:
      summary: Test security
      operationId: querykey
      x-google-backend:
        address: [MY_CLOUD_FUNCTION_2]
      security:
      - api_key_query: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
securityDefinitions:
  # This section configures basic authentication with an API key.
  api_key_header:
    type: "apiKey"
    name: "key"
    in: "header"
  api_key_query:
    type: "apiKey"
    name: "key"
    in: "query"
与您一样,即使没有提供API密钥,我也可以看到对
/foo头的请求传递到后端



我建议您在上查看此问题,以便由适当的GCP工程团队审查。

中的
标题时,
名称应为
x-api-key


谢谢!这很有效。