ChatGPT解决这个技术问题 Extra ChatGPT

Google Maps JavaScript API RefererNotAllowedMapError

We're trying to develop an geoplacement app for one of our clients, and we want first to test it in out own domain.

We have signed for Google Maps JavaScript API, and we have a valid browser key and our domain, www.grupocamaleon.com, has been authorized to use that key.

But we can't make even the easiest example to run without error.

We have, in our domain and with our key, the following demo:

(1) http://www.grupocamaleon.com/boceto/aerial-simple.html

But it doesn't work, and Firebug console says:

"Google Maps API error: Google Maps API error: RefererNotAllowedMapError (link to Google documentation on RefererNotAllowedMapError) Your site URL to be authorized: (1)"

My credential page is missing the possibility of adding referrers to accept, so solutions involving adding referrers are not possible right now.

My credential Page:

https://i.stack.imgur.com/j4pPr.jpg

Why do we get that error? How can we fix it?

Possible duplicate of Google maps API referrer not allowed. Your code works for me (with my key), so there is something wrong with your key, the allowed referrers, or the services enabled to use with it.
But I have not such possibility in my credentials page, as you can see in my answer here. Any hint?
Well, I'm a newbie and I don't know how to use this site corrently yet. Sorry for the inconveniences. I've edited my question, adding information on my credential page. Some solutions cited involve adding referrers, what I can not do at the moment.
If you can't change the referrers for the key, remove it or replace it with one you control. A key is not required, but there are benefits to including one.
I created new key with no APP or API restrictions, it said expiredKey for 10 minutes, then started working. Next I tried adding just Places API, but it is being called from web and says: Google Maps JavaScript API error: RefererNotAllowedMapError even after waiting. Then I tried Maps JavaScript API and it kept saying RefererNotAllowedMapError, tried refreshing the page and it finally started to work after 2 minutes and 40 seconds. So watch the hell out. But WITHOUT anything DOES work for me, and so does Maps JavaScript API only. You just have to wait a lot.

p
powerbuoy

I know this is an old question that already has several answers, but I had this same problem and for me the issue was that I followed the example provided on console.developers.google.com and entered my domains in the format *.domain.tld/*. This didn't work at all, and I tried adding all kinds of variations to this like domain.tld, domain.tld/*, *.domain.tld etc.

What solved it for me was adding the actual protocol too; http://domain.tld/* is the only one I need for it to work on my site. I guess I'll need to add https://domain.tld/* if I were to switch to HTTPS.

Update: Google have finally updated the placeholder to include http now:

https://i.stack.imgur.com/uMAPk.png


I confirm: the only combination that allowed me the use of the API was http://www.---.com/*
This answer is the only one that got my code working. I can confirm that the example that Google provides does not work.
Thank you, @Google, for giving us false information. http://----.com/* is the correct format.
If you have multiple sub domains you can do, http://*.example.com and https://*.example.com to cover all bases
Maybe Google updated that feature – for me it works without the protocol. I have example.com/* in the settings field, and it works from https://example.com/map
R
Rick James

Come on Google, you guys are smarter than the API Credential page lets on. (I know because I have two sons working there.)

The list of "referrers" is far pickier than it lets on. (Of course, it should be more forgiving.) Here are some rules that took me hours to discover:

The order in the list is important. Moving your URL up in the list may make it work.

"http://" prefix is required.

Even "localhost" needs it: "http://localhost/foo/bar.html"

A trailing * as a wildcard seems to work as if it is a string compare.

Even with "http://localhost/foo/bar.html", "http://localhost/foo/bar.html?arg=1" will not work. (Will a wildcard help?)

For both prod dev, have (at least) two rows: "http://localhost/foo/bar.html" and "http://my.site.com/foo/bar.html"

A port number (8085? 4000?) does not seem to be necessary.

There are probably other rules, but this is a tedious guessing game.


About the answer before the edit, I'm running out of patience too. Looks like that wildacards (*) only work in the end of an address (http://my.site.com/*) or in in a subdomain (http://*.site.com). In ALL the other cases it doesn't work
I had incorrectly put a restriction to only allow the Places API (the tab right next to the referrer restrictions). This results in the same RefererNotAllowedMapError error.
Adding www. to my domain name fixed it for me. I am not sure if adding a wildcard (*) to the end of domain, such as (https://www.my-site.com/*) has also corrected the issue.
my ionic mobile app has domain as ionic://app#/tabs. How would I include it in the restrictions. I tried many combinations but in vain. Ex: ionic://app#/tabs, httpionic://app#/tabs, httpsionic://app#/tabs, ionic://*, ionic://app#/*, httpionic://*, ......
@AUK4SO - The pound sign (#) has a special meaning in urls. See <a name=...>. Or did you mean iconic://1234/foo?
D
Daniel Loureiro

Wildcards (asterisks) ARE NOT allowed in the subdomain part.

WRONG: *.example.com/*

RIGHT: example.com/*

Forget what Google says on the placeholder, it is not allowed.


How can I add the following then: sc.maindomain.com/Survey/UpdateInspection/102
You can set as sc.maindomain.com/*
T
TRose

According to the documentation, 'RefererNotAllowedMapError' means

The current URL loading the Google Maps JavaScript API has not been added to the list of allowed referrers. Please check the referrer settings of your API key on the Google Developers Console.

I have the Google Maps Embed API set up for my own personal/work use and thus far have not specified any HTTP referrers. I register no errors. Your settings must be making Google think the URL you're visiting is not registered or allowed.

https://i.stack.imgur.com/9cqCk.png


P
Putu De

Just remind that if you just change it, it may take up to 5 minutes for settings to take effect.


o
ow3n

I tried many referrer variations and waiting 5 minutes as well until I realized the example Google populates in the form field is flawed. They show:

*.example.com/*

However that only works if you have subdomain. or www. in front of your domain name. The following worked for me immediately (omitting the leading period from Google's example):

*example.com/*

Bad idea - someone could create hackexample.com and use your api
@HyderA - A more likely possibility is that someone would register a bunch free google accounts to get extra map loads.
@HyderA they would still need the key, so not a huge chance. But you're right, still a potential risk.
my ionic mobile app has domain as ionic://app#/tabs. How would I include it in the restrictions. I tried many combinations but in vain. Ex: ionic://app#/tabs, httpionic://app#/tabs, httpsionic://app#/tabs, ionic://*, ionic://app#/*, httpionic://*, ......
C
Community

According the google docs this happened because the url on which you are using the Google Maps API, it not registered in list of allowed referrers

EDIT :

From Google Docs

All subdomains of a specified domain are also authorized. If http://example.com is authorized, then http://www.example.com is also authorized. The reverse is not true: if http://www.example.com is authorized, http://example.com is not necessarily authorized

So,Please configure http://testdomain.com domain, then your http://www.testdomain.com will start work.


In my case it was misconfigured DNS for the domain. For example: www.testdomain.com worked fine testdomain.com didn't work causing an error
It would seem the logic of that is reversed? Unless I am missing something.
D
David Robertson

Check you have the correct APIS enabled as well.

I tried all of the above, asterisks, domain tlds, forward slashes, backslashes and everything, even in the end only entering one url as a last hope.

All of this did not work and finally I realised that Google also requires that you specify now which API's you want to use (see screenshot)

https://i.stack.imgur.com/33vos.jpg

I did not have ones I needed enabled (for me that was Maps JavaScript API)

Once I enabled it, all worked fine using:

http://www.example.com/*

I hope that helps someone! :)


D
Darren Murphy

The Problem
Google suggests the format *.example.com/*
This format does not work.

The Solution
Check the browser console for the Google Maps JavaScript API error: RefererNotAllowedMapError
Underneath the error it should have: "Your site URL to be authorized: https://example.com/".
Use that url for the referrer and add a wildcard * to the end of it (https://example.com/*, in this case).


Hey I tried this and every other variation I've found on SO without any luck. Did something change in 2021?
In 2022 this answer is working. https://yoursite.com/* , https://*.yoursite.com/* You must add both of these. This should be work.
h
hiroshi

I found that even your HTTP Referreres are valid enough, wrong set of API Restrictions causes Google Maps JavaScript API error: RefererNotAllowedMapError.

For example:

You are using Javascript API for the key.

Add http://localhost/* to Application Restrictions / HTTP Referrences

Choose Maps Embed API instead of Maps Javascript API

This causes RefererNotAllowedMapError


Yes, in my case misclassification was the issue
But what about the security? Can anyone use my key in localhost if I allowed for localhost ?
d
dakab

There are lots of supposed solutions accross several years, and some don’t work any longer and some never did, thus my up-to-date take working per end of July 2018.

Setup:

Google Maps JavaScript API has to work properly with…

multiple domains calling the API: example.com and example.net

arbitrary subdomains: user22656.example.com, etc.

both secure and standard HTTP protocols: http://www.example.com/ and https://example.net/

indefinite path structure (i.e. a large number of different URL paths)

Solution:

Actually using the pattern from the placeholder: ://*.example.com/*.

Not omitting the protocol, but adding two entries per domain (one per protocol).

An additional entry for subdomains (with a *. leading the hostname).

I had the feeling that the RefererNotAllowedMapError error still appeared using the proper configuration (and having waited ample time). I deleted the credential key, repeated the request (now getting InvalidKeyMapError), created new credentials (using the exact same setup), and it worked ever since.

Adding mere protocol and domain seemed not to have included subdomains.

For one of the domains, the working configuration looks like this:

https://i.stack.imgur.com/b1ZxO.png

(As text:)

Accept requests from these HTTP referrers (web sites)
    https://*.example.com/*
    https://example.com/*
    http://*.example.com/*
    http://example.com/*

In my case, this solution helped but had API Restriction on the wrong API. For some reason, the same error message was returned. Changing API Restriction from 'Places API' to 'Maps Javascript API' fixed the issue.
deleting the key and creating a new one worked for me
O
Owen Masback

None of these fixes were working for me until I found out that RefererNotAllowedMapError can be caused by not having a billing account linked to the project. So make sure to activate your free trial or whatever.


p
php-b-grader

This is another sh1tty Google product with a terrible implemenation.

The problem I have found with this is that if you restrict an API key by IP address, it wont work... BUT far be it from Google to make this point clear... It wasn't until troubleshooting and researching I found:

API keys with an IP addresses restriction can only be used with web services that are intended for use from the server side (such as the Geocoding API and other Web Service APIs). Most of these web services have equivalent services within the Maps JavaScript API (for example, see the Geocoding Service). To use the Maps JavaScript API client side services, you will need to create a separate API key which can be secured with an HTTP referrers restriction (see Restricting an API key).

https://developers.google.com/maps/documentation/javascript/error-messages

FFS Google... Pretty important piece of information that would be good to clarify on setup...


f
flavio.donze

https://i.stack.imgur.com/YOWcO.jpg

Accept requests from these HTTP referrers (web sites)

Write localhost directory path


according to Google's text it is supposed to be an optional property and if it blank it should work
@ayanchakraborty - I have tried 10 different combinations of localhost/index.html (and various paths) as well as various combinations of file:///android_assets/www/index.html - but nothing is working. The RefererNotAllowedMapError in console is showing me: Your site URL to be authorized: __file_url__//android_asset/www/index.html#/tab/map////` - but this and variations don't work either.
J
Jaime Montoya

I experienced the same error:

https://i.stack.imgur.com/TT4sH.png

This link talks about how to set up API key restrictions: https://cloud.google.com/docs/authentication/api-keys#adding_http_restrictions

In my case, the problem was that I was using this restriction:

HTTP referrers (web sites) Accept requests from these HTTP referrers (web sites) (Optional) Use *'s for wildcards. If you leave this blank, requests will be accepted from any referrer. Be sure to add referrers before using this key in production. https://*.example.net/*

This means that URLs such as https://www.example.net or https://m.example.net or https://www.example.net/San-salvador/ would work. However, URLs such as https://example.net or https://example.net or https://example.net/San-salvador/ would not work. I simply needed to add a second referrer:

https://example.net/*

That fixed the problem for me.


P
Pare

I add 2 website domains, set "*" in subdomain is not working but specific subdomain "WWW" and non-subdomain have been worked for my websites using the same Google Map API key.

dont' use "*" in subdomain

Hope it help.


C
Community

That your billing is enabled That your website has been added to Google Console That your website is added to the referrers in your app. (do a wildcard for both www and none www)

http://www.example.com/* and http://example.com/*

That Javascript Maps is enabled and you are using the correct credentials That the website has been added to your DNS to enable your Google Console above. Smile after it works!


Your list of allowed referrers can be found in Google Cloud Platform web console (console.cloud.google.com) under APIs & Services > Credentials > (your key) > Application restrictions > HTTP Referrers
I
ITwrx

you show a screenshot of your api credentials page, but you have to click on "Browser key 1" and go from there to add referrers.


R
Radmation

For deeper nested pages

If you have a project in a folder for example or nested pages

http://yourdomain.com/your-folder/your-page you can enter this in

http://yourdomain.com/*/*

The important part being /*/*/* depending how far you need to go

It seems that the * will not match / or get into deeper paths..

This will give your full domain access, well unless you have deeper nesting than that..


F
FMCorz

I struggled to make this work as well, but here are some pointers:

The URLs set as referrers include http, e.g. http://example.com/*

Google Maps JavaScript API was enabled

Billing was set-up on this account

Once all of this above was resolved, the maps displayed as expected.


a
ahgood

Enable billing for Google project fixed the problem.


S
Shingai Munyuki

http://www.example.com/* has worked for me after days and days of trying.


佚名

I got mine working finally by using this tip from Google: (https://support.google.com/webmasters/answer/35179)

Here are our definitions of domain and site. These definitions are specific to Search Console verification:

http://example.com/ - A site (because it includes the http:// prefix)
example.com/ - A domain (because it doesn't include a protocol prefix)
puppies.example.com/ - A subdomain of example.com
http://example.com/petstore/ - A subdirectory of http://example.com site

b
brad

I was attempting to use the Places API (Autocomplete) and had to also enable the Maps Javascript API from within Google Cloud Console before the Places API would work.


N
Nalaskow

Check your decklaration on site. To load the Google Maps JavaScript API, use a script tag like this

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
async defer></script>

I using this declaration on my Wordpress site in function.php file

wp_enqueue_script("google-maps-v3", "//maps.google.com/maps/api/js?key=YOUR_API_KEY", false, array(), false, true);

I have set API key on this format, and its works fine

http://my-domain-name(without www).com/*

this declaration not working

*.my-domain-name.com/*

R
Robot Boy

Removing the restrictions (to None) worked for me.


removing the restrictions is considered bad practice and makes your API vulnerable.
@mtness yes obviously but the poster has already tried it first without success. Unfortunately Google's documentation isn't any more helpful. Every answer here are bringing in new tips, it's almost like a guessing game...
R
RationalRabbit

In my experience

http://www.example.com

worked fine But, https required /* at the end


K
KJB

Chrome's Javascript console suggested I declare the entire page address in my HTTP referrer list, in this instance http://mywebsite.com/map.htm Even though the exact address is http://www.mywebsite.com/map.htm - I already had wildcard styles listed as suggested by others but this was the only way it would work for me.


T
Tahir77667

This worked for me. There are 2 major categories of restrictions under api key settings:

Application restrictions

API restrictions

Application restrictions:

At the bottom in the Referrer section add your website url " http://www.grupocamaleon.com/boceto/aerial-simple.html " .There are example rules on the right hand side of the section based on various requirements.

https://i.stack.imgur.com/f7UC9.png

API restrictions:

Under API restrictions you have to explicitly select 'Maps Javascript API' from the dropdown list since our unique key will only be used for calling the Google maps API(probably) and save it as you can see in the below snap. I hope this works for you.....worked for me

https://i.stack.imgur.com/3dxoQ.png

Check your Script:

Also the issue may arise due to improper key feeding inside the script tag. It should be something like:

  <script async defer src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&callback=initMap"
  type="text/javascript"></script>

n
nitin.k

If you are working on localhost then do not include http or https in the url.

Use "localhost" without protocols. I struggled for days and found it working.