【发布时间】:2019-06-13 13:34:28
【问题描述】:
我正在尝试按照article 在 Cloud Functions 上配置 Endpoints。
已执行以下步骤:
1) 创建一个谷歌云平台(GCP)项目,并部署如下云函数。
export const TestPost = (async (request: any, response: any) => {
response.send('Record created.');
});
使用以下命令
gcloud functions deploy TestPost --runtime nodejs10 --trigger-http --region=asia-east2
到这里为止,功能运行良好。
2) 使用以下命令将 ESP 容器部署到 Cloud Run
gcloud config set run/region us-central1
gcloud beta run deploy CLOUD_RUN_SERVICE_NAME \
--image="gcr.io/endpoints-release/endpoints-runtime-serverless:1.30.0" \
--allow-unauthenticated \
--project=ESP_PROJECT_ID
ESP 容器也已成功部署。
3) 创建一个描述 API 的 OpenAPI 文档,并配置到 Cloud Functions 的路由。
swagger: '2.0'
info:
title: Cloud Endpoints + GCF
description: Sample API on Cloud Endpoints with a Google Cloud Functions backend
version: 1.0.0
host: HOST
schemes:
- https
produces:
- application/json
paths:
/Test:
get:
summary: Do something
operationId: Test
x-google-backend:
address: https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/Test
responses:
'200':
description: A successful response
schema:
type: string
4) 使用以下命令部署 OpenAPI 文档
gcloud endpoints services deploy swagger.yaml
5) 配置 ESP,以便它可以找到 Endpoints 服务的配置。
gcloud beta run configurations update \
--service CLOUD_RUN_SERVICE_NAME \
--set-env-vars ENDPOINTS_SERVICE_NAME=YOUR_SERVICE_NAME \
--project ESP_PROJECT_ID
gcloud alpha functions add-iam-policy-binding FUNCTION_NAME \
--member "serviceAccount:ESP_PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
--role "roles/iam.cloudfunctions.invoker" \
--project FUNCTIONS_PROJECT_ID
这样就成功了
6) 向 API 发送请求
工作得很好。
现在我想实现身份验证,所以我对 OpenAPI 文档进行了以下更改
swagger: '2.0'
info:
title: Cloud Endpoints + GCF
description: Sample API on Cloud Endpoints with a Google Cloud Functions backend
version: 1.0.0
host: HOST
schemes:
- https
produces:
- application/json
security:
- client-App-1: [read, write]
paths:
/Test:
get:
summary: Do something
operationId: Test
x-google-backend:
address: https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/Test
responses:
'200':
description: A successful response
schema:
type: string
securityDefinitions:
client-App-1:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
scopes:
read: Grants read access
write: Grants write access
x-google-issuer: SERVICE_ACCOUNT@PROJECT.iam.gserviceaccount.com
x-google-jwks_uri: https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT@PROJECT.iam.gserviceaccount.com
我使用以下命令创建了一个服务帐户。
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME --display-name DISPLAY_NAME
使用以下方法将令牌创建者角色授予服务帐户
gcloud projects add-iam-policy-binding PROJECT_ID --member serviceAccount:SERVICE_ACCOUNT_EMAIL --role roles/iam.serviceAccountTokenCreator
重新部署 OpenAPI 文档
gcloud endpoints services deploy swagger.yaml
现在当我测试 API 时出现以下错误
{
"code": 16,
"message": "JWT validation failed: BAD_FORMAT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"stackEntries": [],
"detail": "auth"
}
] }
我正在使用 BearerToken 将通过 gcloud 生成的访问令牌传递到请求中
生成访问令牌的cmd是gcloud auth application-default print-access-token
有人能指出这里的问题吗。谢谢...
编辑#1: 我正在使用 Postman 连接到我的 API
使用以下命令后,我得到一个不同的错误。
命令:
gcloud auth print-identity-token SERVICE_ACCOUNT_EMAIL
错误:
{
"code": 16,
"message": "JWT validation failed: Issuer not allowed",
"details": [
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"stackEntries": [],
"detail": "auth"
}
]
}
【问题讨论】:
-
我认为问题出在以下,但我不确定这里应该是什么
x-google-issuer & x-google-audiences -
您遗漏了重要的部分。导致错误的代码。编辑您的问题并展示您如何使用
gcloud生成的令牌。 -
@John Hanley,我发现了这个问题。我正在生成一个访问令牌并使用它来访问 api。你需要的是一个签名的 jwt 令牌。如果不行,我会尝试生成一个和 c。
-
顺便说一句,我正在通过 Postman 访问 API...
-
我可能是错的,我在这里使用我的记忆。我不认为你想要一个访问令牌,我认为你想要一个身份令牌。 cloud.google.com/sdk/gcloud/reference/auth/print-identity-token
标签: google-cloud-platform google-cloud-functions google-cloud-endpoints