簡體   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