You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用AWS API Gateway GetApiKeys的getValue()返回null,如何获取实际密钥?

如何通过AWS API Gateway Java SDK获取实际的API密钥值

这个问题我之前也碰到过,其实是因为AWS API Gateway的GetApiKeys接口默认不会返回API密钥的实际值——毕竟这是敏感信息,AWS默认会隐藏它来保障安全。

要解决这个问题,你只需要在构建GetApiKeysRequest的时候,调用includeValues(true)方法明确告诉API你需要返回密钥的实际值。

修改后的代码示例

AmazonApiGateway client = AmazonApiGatewayClientBuilder.standard()
    .withRegion("my region")
    .build();

GetApiKeysRequest req = new GetApiKeysRequest();
// 关键:设置包含密钥实际值
req.setIncludeValues(true);

GetApiKeysResult keys = client.getApiKeys(req);
ApiKey key = keys.getItems().get(0); // 获取第一个密钥

System.out.println(key.getId());
System.out.println(key.getName());
System.out.println(key.getValue()); // 现在这个方法会返回实际的密钥值了

额外注意事项

  • 权限要求:确保你的IAM实体(用户/角色)拥有apigateway:GetApiKeys的权限,否则会收到权限错误。
  • 安全提醒:API密钥是敏感信息,不要在日志中打印、硬编码或者随意存储,处理完后要及时清除内存中的敏感数据,避免泄露风险。

内容的提问来源于stack exchange,提问作者Baked Inhalf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:41:54