Call to Directions API failing with "REQUEST_DENIED" status

The network response from the Google Directions API is returning an error:

{"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"}

I believe the networking code is correct:

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:

Additionally, the same API key has been and continues to be used for the Google Places SDK:

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

Why is the Directions API returning an error?

1 answer