繁体   English   中英

呼叫方向 API 失败,状态为“REQUEST_DENIED”

[英]Call to Directions API failing with "REQUEST_DENIED" status

来自 Google Directions API 的网络响应返回错误:

{“error_message”:“此 IP、站点或移动应用程序未被授权使用此 API 密钥。请求从 IP 地址 xxx.xx.x.xx 收到,引用为空”,“路由”:[],“状态” :“请求被拒绝”}

我相信 .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)))                                                                                                                                                
        }                                                                                                                                                                                                              
    }                                                                                                                                                                                                                  
})                                                                                                                                                                                                                     
}                                                                                                                                                                                                                          

..并且 API 凭证似乎配置正确:

此外,相同的 API 密钥已经并将继续用于 Google Places SDK:

// Initialize Places SDK.
Places.initialize(applicationContext, ".....")

为什么 Directions API 返回错误?

在您的示例中,您没有使用 Android SDK,但是有一个密钥限制集指定您是。

我建议代理路线服务,以便您的密钥无法从您的应用程序中提取。

请参阅android 中的使用 Google 地图方向 API - 需要什么样的 API 密钥?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM