API 密钥是一个简单的字符串,用于标识Google Cloud 项目,以实现配额、结算和监控的目的。开发者在 Google Cloud 控制台中为项目生成一个 API 密钥,并将该密钥作为查询参数嵌入到对 API 的每次调用中或嵌入到请求标头中。
如果您在 API 配置中指定 API 密钥要求,API Gateway 会使用 API 密钥来查找关联的 Google Cloud 项目。除非 API 密钥是在您的 Google Cloud 项目或已启用 API 的其他Google Cloud 项目中生成的,否则 API Gateway 将拒绝请求。
如需创建 API 密钥或查看项目中已有的 API 密钥,请依次前往 API 和服务 > 凭据页面。 Google Cloud
如需使用配额等 API Gateway 功能,您可以传入 API 密钥,以便 API Gateway 可以识别与客户端应用关联的 Google Cloud 项目。
关于 API Gateway 的 API 密钥身份验证
如果您使用 API 密钥进行身份验证,则必须先为您的服务启用 API 密钥支持。
输入以下命令:
gcloud services enable MANAGED_SERVICE_NAME
MANAGED_SERVICE_NAME 指定您在部署 API 时创建的代管式服务的名称。您可以在控制台的 API 着陆页上的 API 的“托管式服务”列中找到此名称。或者,您也可以在使用 gcloud api-gateway apis describe 命令时在 Managed service 属性中找到此名称。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-04-22。"],[[["API keys are simple strings used to identify a Google Cloud project for quota, billing, and monitoring in API Gateway."],["API Gateway requires API keys generated within your Google Cloud project, or projects where your API is enabled, to authorize requests."],["While useful for features like quotas, API keys are vulnerable to man-in-the-middle attacks and should not be the sole form of authentication for sensitive data."],["Enabling API key support for a service requires using the `gcloud services enable MANAGED_SERVICE_NAME` command, specifying the managed service's name."],["For security, API keys should be restricted to specific APIs through the Google Cloud console's **APIs & Services \u003e Credentials** page, ensuring they are not used by unauthorized parties."]]],[]]