0

在遵循设置 GCP 的 API Gateway的文档时,我遇到了一个问题,当我调用端点时,如下所示:

curl --request POST 'https://my-dev-project-XXX.wl.gateway.dev/helloWorld?key=XXX'

它返回一个 HTML 页面以通过 Google 登录进行身份验证,而不是以下相应响应:"Hello World!"

函数名有问题?

我知道 Cloud FunctionhelloWorld存在,因为如果我将上面的 cURL 请求更改为:

curl --request POST 'https://my-dev-project-XXX.wl.gateway.dev/helloWorldButChangeTheName?key=XXX'

它返回:

{"message":"The current request is not defined by this API.","code":404}

API 密钥有问题?

我知道 API 密钥是有效的,因为如果我将其更改为YYY,我会得到:

{"code":400,"message":"INVALID_ARGUMENT:API key not valid. Please pass a valid API key."}

请求方法有问题?

我知道 POST 的 Request 方法是正确的,因为如果我将其更改为 GET,它会返回:

{"message":"The current request is matched to the defined url template \"/helloWorld\" but its http method is not allowed","code":405}

授权问题?

Cloud Functions 通常有一些类似的 StackOverflow 解决问题[1][2];但是,这不是同一个问题。我知道这一点是因为我已经让实际的 Cloud Function 无需授权即可公开访问。所以如果我打电话:

curl --request POST 'https://us-west2-my-dev-project.cloudfunctions.net/helloWorld'

我回来了"Hello World!"

服务帐户角色有问题?

按照为网关配置服务帐户的文档,我确保设置了两个角色:

  • 服务帐号用户
  • 云函数调用者

如果我没有正确设置这些设置,我不确定它会是什么样子(因为我在得出这里可能有问题的结论之前找到了答案),但这些设置应该足够了。

API 配置文件

我与文档教程的唯一“显着”区别是我的配置文件,即:

swagger: '2.0'
info:
  title: XXX
  description: Sample API on API Gateway with a Google Cloud Functions backend
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
paths:
  # different name than tutorial
  /helloWorld:
    post:
      summary: Greet a user
      # different id than tutorial
      operationId: helloWorld
      x-google-backend:
        # different endpoint than tutorial
        address: https://us-central1-my-prod-project.cloudfunctions.net/helloWorld
      security:
        - api_key: []
      responses:
        '200':
          description: A successful response
          schema:
            type: string
securityDefinitions:
  api_key:
    type: apiKey
    name: key
    in: query
4

1 回答 1

0

谷歌登录屏幕在这里引起了很多混乱,因为这个问题似乎不会产生这样的结果。你会认为这是身份验证的问题,但事实并非如此。相反,API 配置文件有两个问题,都与address

  1. 该区域不同于云函数的区域。Cloud Function 部署在us-west2API 配置文件中us-central1(文档中显示的内容)。
  2. 项目 ID 与 Cloud Function 的不同。Cloud Function 的 ID 为 ,my-dev-project而 API 配置文件将其设置为my-prod-project. 这是因为我有两个不同的环境,我在尝试这个并且把它搞砸了。

我想 API 配置文件的其他细微问题也可能导致响应是 Google 登录页面,所以如果我上面的两点没有解决您的问题,请查找其他差异,例如路径名和operationId.

于 2022-03-04T15:49:29.663 回答