来自 Google Directions API 的网络响应返回错误:
<块引用>{"error_message":"此 IP、站点或移动应用程序无权使用此 API 密钥。从 IP 地址 xxx.xx.x.xx 收到请求,引用为空","routes":[], "status":"REQUEST_DENIED"}
我相信网络代码是正确的:
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)))
}
}
})
}
..并且 API 凭据似乎已正确配置:
此外,相同的 API 密钥已经并将继续用于 Google Places SDK:
// Initialize Places SDK.
Places.initialize(applicationContext, ".....")
为什么 Directions API 返回错误?
答案 0 :(得分:1)
在您的示例中,您没有使用 Android SDK,但有一个指定您使用的密钥限制集。
我建议代理路线服务,以便无法从您的应用中提取您的密钥。
见Using Google Maps Directions API in android - what kind of API key is needed?