Search code examples
javascriptajaxgoogle-geolocation

Google Geolocation API: 403 POST Forbidden error


I'm using Google's geolocation api, but getting this error:

"POST http://www.googleapis.com/geolocation/v1/geolocate?key= //my API key... 403 (Forbidden)"

This is a brand new API key, so I can't imagine I'm hitting my daily limit...

    function GeoLocate() {
       var QueryURL = 
           "http://www.googleapis.com/geolocation/v1/geolocate?key=" + 
            GeolocationAPIKey;
        return new Promise(function(resolve, reject) {

        $.ajax({
            method: "POST",
            url: QueryURL,
        }).done(function(response) {
            resolve(response);
        }).fail(function(err) {
            reject(err);
        })
       })
          console.log(response);
       }

Solution

  • Google's geolocation API requires https rather than http.

    So code from above is corrected by just adding an 's' to line 3:

     function GeoLocate() {
       var QueryURL = 
           "https://www.googleapis.com/geolocation/v1/geolocate?key=" + 
            GeolocationAPIKey;
        return new Promise(function(resolve, reject) {
    
        $.ajax({
            method: "POST",
            url: QueryURL,
        }).done(function(response) {
            resolve(response);
        }).fail(function(err) {
            reject(err);
        })
       })
          console.log(response);
       }