While applying application restrictions with 'Websites' option in Google cloud console for Map API key, it stopped working even in the allowed web url also.
Followed below steps:
- Enabled 2 API from API & Services (Maps JavaScript API, Geocoding API )
- Created API Key from Keys & Credentials
- Applied application restriction as we want to use this key from my hosted web page.
And We have below javascript snippet to call geocode api from client web app.
const latitude = 24.4564699;
const longitude = 54.4007989;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
try {
const data = JSON.parse(response);
if (data.status === "OK" && data.results && data.results.length > 0) {
const formattedAddress = data.results[0].formatted_address;
console.log("formattedAddress" + formattedAddress);
} else {
console.log("Location not found");
}
} catch (error) {
console.log("Error parsing response");
console.error(error);
}
}
};
let url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=<MAP_API_KEY>`;
xhttp.open("GET", url, true);
xhttp.send();
But we are getting request_denied response.
{
"error_message" : "API keys with referer restrictions cannot be used with this API.",
"results" : [],
"status" : "REQUEST_DENIED"
}
The web service which you are using can't use the key with referrer restrictions. Use the geocoder that is part of the JavaScript API v3 with the key with referrer restrictions.
proof of concept fiddle (modified from example in docs)