API key Browser API keys cannot have referer restrictions when used with this API

Damian picture Damian · Feb 10, 2017 · Viewed 57.4k times · Source

I'm trying to program geocoding. I created API key, but when I add it into my website code I don't get anything, however when I don't use API everything works well. When I'm sending:

https://maps.googleapis.com/maps/api/geocode/json?address=".$adres."&key=KLUCZ_XXX

I get a response:

Browser API keys cannot have referer restrictions when used with this API.

If I use:

https://maps.googleapis.com/maps/api/geocode/json?address=".$adres" 

The result is correct but the number of queries is limited per day.

Key constraint: referrals HTTP (Web) - set to the domain from which conjure inquiry. Interface Google Maps Geocoding API is turned on.

Does the API key has to be activated also somewhere else to work?

Answer

Raymond picture Raymond · Feb 25, 2017

It sounds like you may be making the API call server side. I ran into this issue earlier today. Since you've placed a referrer restriction on your API key, it will be limited to executing on the browser with the web service APIs, including the Geocoding API. It didn't work for me even when manually setting the referrer in the request headers. You can find the other web service APIs on this page: https://developers.google.com/maps/web-services/

Important: If you are using any of the web service APIs with an API key that has referer restictions, your requests will fail with the error message: "API keys cannot have referer restrictions when used with this API." You should switch to using a server restriction.

You'll want to create a separate key to use server-side. You can change your restriction from a browser restriction to a server restriction by using IP addresses to restrict access, instead of browser referrers.

See this section in the Maps APIs FAQ on switching key type to a server restricted key: https://developers.google.com/maps/faq#switch-key-type

Alternatively, you can continue using a browser key and geocode client-side (JavaScript), then return the result to the backend via ajax.