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

ApiMapsGeocoding

Api Problem Overview


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?

Api Solutions


Solution 1 - Api

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.

Solution 2 - Api

If server-side geocoding is not an option, you should use the geocoder from the Google Javascript API. You can set HTTP referer restrictions on that API.

Google itself says to avoid the Non-Javascript Geocoder API for dynamic geocoding:

> This service is generally designed for geocoding static > (known in advance) addresses for placement of application content on a > map; this service is not designed to respond in real time to user > input. For dynamic geocoding (for example, within a user interface > element), consult the documentation for the Maps JavaScript API client > geocoder and/or the Google Play services Location APIs.

Solution 3 - Api

This issue happens when you set the referrer for the server key to the domain name instead of the IP address. Then it will show this message:

> API keys with referer restrictions cannot be used with this API.

For geocode API, create a separate API key and restrict that API using the IP address. Doing that worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDamianView Question on Stackoverflow
Solution 1 - ApiRaymondView Answer on Stackoverflow
Solution 2 - ApiSlashView Answer on Stackoverflow
Solution 3 - ApimljView Answer on Stackoverflow