在遵循设置 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