5

we are using the Google Maps API on our website to cross-check users entries of addresses.

A typical call would be: https://maps.googleapis.com/maps/api/geocode/json?key=XXXX&address=12-6 Daly St, Maryborough, VIC, Australia , 3465

Where XXXX is of course the key taken from the credentials.

We have set up the credentials in the developers console. Maps JavaScript API, Geocoding API, Geolocation API, and others are enabled.

The issue that we are having is that we are not able to restrict the key with "HTTP referrers (web sites)" nor with "IP addresses (web servers, cron jobs, etc.)". The result is that we have to leave the "Application Restriction" to "none".

As soon as we restrict this key, an error is returned:

With "HTTP referrers (web sites)", the error is:
"API keys with referer restrictions cannot be used with this API."

With "IP addresses (web servers, cron jobs, etc.)", the error is:
This IP, site or mobile application is not authorized to use this API key. Request received from IP address XXXXX

Could anyone help us in getting the right settings for this to work? We also have realised that the API fees have increased dramatically in the last month, which could indicate a theft of quota, making it imperative to restrict the keys.

Also, could there be another reason for the increase in fees for last month?

EXPECTED BEHAVIOUR: the API returns the JSON information with latitude, longitude etc.., which is working if we haven't set restrictions.

ACTUAL BEHAVIOUR: When adding the restriction though, the JSON response is an error with the message mentioned above.

UPDATE

I was calling the URL above with Javascript via ajax, having async set to false. I change the call following the description shown here: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Restrictions (HTTP referers) seem to be working fine now.

My issue is that I can return logs and values when address is correct, but nothing happens when the address is wrong.

I have the following in my code:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY-KEY&callback=clf_init"></script>
<script>
var geocoder;
function clf_init() {
    geocoder = new google.maps.Geocoder();
}

function submit(){
    var address = document.getElementById('address').value;
    geocoder.geocode({ 'address': address }, function(results, status) 
    {
        console.log("STATUS: " +status);
        console.log("RESULT: " + JSON.stringify(results));
        if (status==='OK') {
            console.log("SUCCESS");
        }
        else {
            console.log("ERROR");
        } 
    });
}

As mentioned, if the address is correct, I am getting the "STATUS: OK" log, but if the address is incorrect, I don't get any log at all, as if the function was not even executed. Any idea why this is and how I should change the code to handle error?

Admino
  • 69
  • 1
  • 3
  • 1
    Are you only using the key in calls like the one in your question (`https://maps.googleapis.com/maps/api/geocode/json?key=XXXX&address=12-6`)? That is a web service, and requires the calls be made from the server with an IP restriction (so you shouldn't get the first error) – geocodezip Jan 31 '19 at 14:05
  • possible duplicate of [Request Denied by Google API: API keys with referer restrictions cannot be used with this API](https://stackoverflow.com/questions/48161783/request-denied-by-google-api-api-keys-with-referer-restrictions-cannot-be-used) – geocodezip Jan 31 '19 at 14:07
  • possible duplicate of [Google Maps Key Restrictions by IP Address not working](https://stackoverflow.com/questions/48134596/google-maps-key-restrictions-by-ip-address-not-working) – geocodezip Jan 31 '19 at 14:08
  • If you execute the geocode requests on client side you should consider switching to the [geocode service](https://developers.google.com/maps/documentation/javascript/geocoding) of Maps JavaScript API. In this case you can use an API key with HTTP referrer restriction. As already mentioned @geocodezip the direct call to web service is supposed to be executed on server side only. – xomena Jan 31 '19 at 20:48
  • Thank you so much for your tips! Please see my update in the main post.. – Admino Feb 05 '19 at 15:27
  • Nothing in the restrictions panel indicates that you must use IP rather that URL restrictions but I discovered it quite my accident that it is necessary. However, the panel does not seem to save the IP. If I save, then open it again, it shows no restriction. – DonP Feb 28 '19 at 04:52

0 Answers0