如何通过GeoApiContext向Google Geocoding API发送签名请求?
解决GeoApiContext发送签名请求的方法
要通过签名请求提升Google Geocoding API的调用限额,你需要在GeoApiContext的构建过程中同时配置API密钥与签名密钥,具体操作如下:
1. 准备签名密钥
先在Google Cloud控制台生成签名密钥(通常为.p12或.pem格式文件),并确保你的API密钥已开启签名验证功能。
2. 修改代码配置签名
调整GeoApiContext的构建逻辑,添加签名相关配置,替换成你自己的签名密钥路径和密码:
String googleApiKey = getGoogleApiKey(); // 替换为你的签名密钥文件路径与密码 File privateKeyFile = new File("/path/to/your/private-key.p12"); String privateKeyPassword = "your-private-key-password"; GeoApiContext geoApiContext = new GeoApiContext.Builder() .apiKey(googleApiKey) .signature(new PrivateKeySignature(privateKeyFile, privateKeyPassword)) .build(); LatLng latLng = buildLatLong(userLocation); GeocodingApiRequest geocodingApiRequest = GeocodingApi.newRequest(geoApiContext).latlng(latLng); long startTime = Instant.now().toEpochMilli(); try { GeocodingResult[] geocodingResult = geocodingApiRequest.await(); // 后续结果处理逻辑 } catch (Exception e) { // 异常处理逻辑 }
关键注意点
- 确保签名密钥文件路径正确,且程序拥有文件读取权限
- 签名密钥的密码需与生成时设置的保持一致
- 启用签名验证后,API调用限额会提升至更高额度(具体以Google Cloud控制台的配额说明为准)
内容的提问来源于stack exchange,提问作者Harshit Vijayvargia
相关产品推荐
相关产品推荐

