Search code examples
google-mapsurlgeocoding

google maps api, The provided API key is invalid


This is my first time posting a question on stack overflow, really hoping you guys can help.

I am trying to use the google maps geocodeing api for the first time and cannot get it to work.

The url format I am using is:

https://maps.googleapis.com/maps/api/geocode/json?address=LS2+9AQ&sensor=false&key=[MY_API_KEY]

In this case I am looking up the uk postcode: LS2 9AQ from a chrome browser.

The url does work when I don't use my api key but the limit on the number of allowed calls without the key is too low for my needs.

I am not yet using this in a production site, for now it is running from my local dev in chrome.

The actual response I get is:

[error_message] => The provided API key is invalid.
[results] => Array
    (
    )

    [status] => REQUEST_DENIED

When I remove the api key it works and returns the following:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "LS2 9AQ",
               "short_name" : "LS2 9AQ",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Woodhouse",
               "short_name" : "Woodhouse",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Leeds",
               "short_name" : "Leeds",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "West Yorkshire",
               "short_name" : "West York",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "Leeds",
               "short_name" : "Leeds",
               "types" : [ "postal_town" ]
            }
         ],
         "formatted_address" : "Woodhouse, Leeds, West Yorkshire LS2 9AQ, UK",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 53.8090296,
                  "lng" : -1.5509549
               },
               "southwest" : {
                  "lat" : 53.8083045,
                  "lng" : -1.5519655
               }
            },
            "location" : {
               "lat" : 53.808447,
               "lng" : -1.5517752
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 53.81001603029149,
                  "lng" : -1.550111219708498
               },
               "southwest" : {
                  "lat" : 53.80731806970849,
                  "lng" : -1.552809180291502
               }
            }
         },
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

I have tried replacing the url with one that is currently in use on my works website and I got the same response, I have tried two different keys that I generated myself, one created last week and the other created an hour ago. I have set up the keys in the following way:

  • payment is enabled and a credit card is attached to the project (I was told this was needed to increase limit on api's by validating identity)
  • on one key referrer is set to "dev.test.com/*" (this is the url I am using on my local dev for this). On the other key no referrer is specified.
  • the key is a browser apps key

I am definitely entering the key properly, both me and another developer have checked this multiple times, does anyone have any ideas on what could be wrong?

Thanks in advance to anyone that helps.

UPDATE I have found the problem that was causing the process to stop working after 10 iterations. The google geocoding api has a limit of 10 calls per second in place and I was going over this, I have now added in a 1 second delay every 10 iterations and I can now in theory reach the daily limit.

However I still need more calls than the default free limit, as described in my comments below I believe I can use an api key to increase the free limit based on me having a verified account. I am using this for business but it is not high enough volume to be worth paying for it. Can anyone help clear this up for me?


Solution

  • Ok, It looks like what I was trying to do is impossible without paying for a business account. There is no way to apply an apikey to the google geocoding api.

    I had misunderstood some documentation regarding the google maps api which referred to raising the allowed limit on daily queries.

    This does not seem to apply to the geocoding api.