Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API KEY referral wildcard for localhost development

Google advises to specify which domains the browser key is allowed to be used from.

enter image description here

While this is definitely a great feature, I would like for the key to keep working while I'm developing the website on localhost.

I guess one sure way is to just not specify an API_KEY at all in development environment (google maps work locally without it).

Still this is not always convenient.

I would like to find out exactly what is the correct way to specify the wildcard for the key to work locally.
I have achieved this before by defining a whole bunch of wildcards in hopes that one of them would work. And, well, some of them did, because it worked eventually.

Since google says it takes for about 5 minutes for the referral changes to take effect I had never had the patience to find out which card exactly had done the job.
And google doesn't provide any visuals as to which card would match what domains.

Here someone says that API_KEY works for localhost/ by default. But of course I have my local server at some port other than :80. In this case that person suggests to specify the wildcard with the port like this: http://localhost:1234.

This is very undesired. If I change the port I'm working I surely won't remember that some API_KEY relies on it.

So... what is the correct way to specify a referral for localhost for any port?

localhost:* ?
localhost:*/* ?
*localhost:* ?
http://localhost:* ?
localhost\:* ?

...Does someone know?


Another thing which might be obvious for some but... I'm not sure. Does a wildcard *.example.com/* authorize the url of type http://example.com/some/long/path/?
like image 223
timetowonder Avatar asked Jul 20 '16 13:07

timetowonder


People also ask

Can we use Google Map API in localhost?

in the form after "My web site URL:". The API key they give you works for your localhost.

How can I get Google Map API key for development?

Go to the Google Maps Platform > Credentials page. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key. Click Close.


1 Answers

There is no wildcard for port, so don't use it. Simply omit any reference to port and you should be fine.

I'm using a Cordova Meteor app that embeds a web server (for local storage) running on localhost with a random port assigned by Meteor at runtime. There is no way to know what port Cordova will pick up and on any instance (device running the app) can be a different one.

I've tried many variations like localhost:* or localhost:*/* to no avail.

Eventually I tried a simple localhost and it worked. Now all my requests to http://localhost:<SOME_RANDOM_PORT>/<SOME_RANDOM_PATH> work.

So, go to Google API Manager - Credentials - API key - in the "Accept requests from these HTTP referrers (web sites)" enter localhost and hit Save.

Just a note. This solution whitelists all requests on localhost on any path. You can't add anything after the localhost entry as that seems to brake the rule.

like image 177
Marko Dvornik Avatar answered Oct 13 '22 12:10

Marko Dvornik