我正在尝试通过 Google Cloud API Gateway 中的 JWT 实现用户身份验证。我已根据文档
在 API 配置中配置了安全要求对象和安全定义对象
securityDefinitions:
google_id_token:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://accounts.google.com"
x-google-jwks_uri: "https://www.googleapis.com/oauth2/v3/certs"
security:
- google_id_token: []
后端服务是 Cloud Run 服务
x-google-backend:
address: https://my-apis-fskhw40mta-uk.a.run.app
但是,当我使用我的用户持有者令牌调用 API 时,我收到 403 错误,并且堆栈驱动程序日志中出现此错误
"jwt_authn_access_denied{Audiences_in_Jwt_are_not_allowed}"
调用 API 的 Python 客户端代码是
id_token = subprocess.run(['gcloud', 'auth', 'print-identity-token'], capture_output=True, text=True, shell=True).stdout
http = urllib3.PoolManager()
encoded_args = urlencode({'arg1': "val1"})
r = http.request(
'GET',
API_URL + "/run-api?" + encoded_args,
headers={"Authorization": f"Bearer {id_token}"}
)
使用用户帐户(不是服务帐户)时包含受众的正确方法是什么
更新 1
我找到了一种方法,但我不确定它是否正确。如果我添加32555940559.apps.googleusercontent.com
到securityDefinitions
so 它看起来像这样
securityDefinitions:
google_id_token:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://accounts.google.com"
x-google-jwks_uri: "https://www.googleapis.com/oauth2/v3/certs"
x-google-audiences: "https://oauth2.googleapis.com/token, 32555940559.apps.googleusercontent.com"
它将允许未经身份验证访问 Cloud Run,但是我仍然无法在启用身份验证的情况下调用 Cloud Run。403
由于 API 网关服务帐号没有权限,Cloud Run 返回错误 - 它有Cloud Run Invoker
除了授予权限之外,我需要做些什么才能让 API Gateway 调用云运行吗?Cloud Run Invoker