[英]Call to Directions API failing with "REQUEST_DENIED" status
The.network response from the Google Directions API is returning an error:来自 Google Directions API 的网络响应返回错误:
{"error_message":"This IP, site or mobile application is not authorized to use this API key. Request received from IP address xxx.xx.x.xx, with empty referer","routes":[],"status":"REQUEST_DENIED"}
{“error_message”:“此 IP、站点或移动应用程序未被授权使用此 API 密钥。请求从 IP 地址 xxx.xx.x.xx 收到,引用为空”,“路由”:[],“状态” :“请求被拒绝”}
I believe the.networking code is correct:我相信 .networking 代码是正确的:
override suspend fun getDirections(origin: LatLng, destination: LatLng): Result<JSONObject> = suspendCoroutine { continuation ->
val request = Request.Builder()
.url("https://maps.googleapis.com/maps/api/directions/json?origin=${origin.latitude},${origin.longitude}&destination=${destination.latitude},${destination.longitude}&key=......")
.build()
okHttpClient.get().newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
continuation.resume(
Failure(CustomError(e.localizedMessage ?: genericErrorMsg, true, error = e)))
}
override fun onResponse(call: Call, response: okhttp3.Response) {
if (response.isSuccessful && response.body != null) {
val jsonObject = JSONObject(response.body!!.string())
continuation.resume(Success(jsonObject))
} else {
continuation.resume(Failure(CustomError(response.message)))
}
}
})
}
..and the API credentials appear to be configured appropriately: ..并且 API 凭证似乎配置正确:
Additionally, the same API key has been and continues to be used for the Google Places SDK:此外,相同的 API 密钥已经并将继续用于 Google Places SDK:
// Initialize Places SDK.
Places.initialize(applicationContext, ".....")
Why is the Directions API returning an error?为什么 Directions API 返回错误?
In your example, you are not using an Android SDK, but have a key restriction set specifying that you are.在您的示例中,您没有使用 Android SDK,但是有一个密钥限制集指定您是。
I would recommend proxying the directions service so that your key cannot be extracted from your app.我建议代理路线服务,以便您的密钥无法从您的应用程序中提取。
See Using Google Maps Directions API in android - what kind of API key is needed?请参阅android 中的使用 Google 地图方向 API - 需要什么样的 API 密钥?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.