-
-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a restricted Google Maps API key yields no results-- what referrer is the geocoding request coming from? #269
Comments
I've got exactly the same problem. As far as I understand there are two different Google Geocoding API versions: a server-side version (which should not be used in a client-side project, because you can't protect your API key) and a client-side version, which is part of the Maps JavaScript API. Does anybody know what version is used for Also see this Stackoverflow answer. |
I've done some research and it seems that Important warning: don't use You will be billed for all the API requests they generate! As far as I understand the only solution is to rewrite Or am I missing something and is there another solution? |
@jonoyuan I believe that you can restrict by IP addresses for Google's geocoding service, so that would be an option if you know that your client has a fixed IP (or fixed within a known range). Having said that, I must emphasize that, as @Boschman pointed out, Google discourages the use of the geocoding API for client-side 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." https://developers.google.com/maps/documentation/geocoding/overview So yeah, the right thing to do would be to rework the Google provider in this project to use the JS API instead. (I had no idea about any of this until I started trying to use the Google provider and ran into the same issues that you did when trying to restrict my key) |
I do not have time to rewrite the Google Provider on the short term, and removing it from the source would break peoples applications. Does anyone here have time to look into this and have it use a different API? |
Looks like something critical. I'll try to take a look it next weekend, if no one takes it. But I can't guarantee anything, since I don't have much time myself. |
@smeijer Thank you so much for all of your hard work on this project! Seems like we're all in the same boat - the boat in which there is precious little time... I did start to experiment with this yesterday, but the JS/TS development environment is not my strong suit, so I can't say I was able to make much progress. Someone with more experience building TS libraries would probably be in a better position to wrap this up quickly and well - so anyone reading this should not assume that I'm confident in my ability to successfully resolve this. In the meanwhile, here's what I've been able to figure out so far - To make use of the Google Maps JS API geocoder, it's a little more involved than just using a different endpoint. Of course, there is a different request URL - for example
but Google will block calls to this endpoint from the browser, returning the error "Google Maps JavaScript API library must be loaded" (however, calls to this endpoint from a client like Postman are successful). Perhaps there's a clever way around that, but it seems that it will be necessary to include the JS API package - a little info on that https://developers.google.com/maps/documentation/javascript/overview. And an otherwise handy resource with a JS geocoding example - https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple |
Yeah, I remember that I ran into that when I created the current Google Provider. If possible, I'd like to prevent any Google scripts from being loaded. If that's for some reason not possible, then we should at least make it lazy-loaded (load when rendering the Provider, and make sure it isn't included in /dist bundles). Let's share any findings here so that over time we collect enough info for someone to fix it in the time they have. It would be a waste if multiple people end up having half-finished branches, that are never submitted. Better make it a team effort ❤️ @yuriizinets thanks! No pressure 🙂 |
I agree - it'd be great to not have to depend on loading anything else from Google. Team effort 👍 !!! |
I don't think that bypassing Google requirements is a great idea. I had some experience with bypassing google's translate service limitations. Any solution, that uses unofficial API, is a temporary solution. Even google's official libs/APIs are changing frequently. Supporting unofficial way will require a lot of time and will lead to unobvious situations. Google generates callback name and token for request from input. With this 2 parameters google knows when you're trying to use API directly. Theoretically it's possible to reverse google's JS to find out how callback name and token are generating, but it's really hard. Digging into obfuscated JS is a real pain, I tried :) So, I see 2 options:
@smeijer, so, the choice is yours :) |
Thanks for the explanation. Giving the user full control, means it will be a breaking change. Thereby I think that the best route is to dynamically load it. |
@smeijer I've finally gotten a few free moments to start looking at this, though with JS/TS not being my strong suit, I'm a bit stuck, was wondering if you could help out. After researching a bit, I did notice that there's an NPM module that could help out here - https://github.com/googlemaps/js-api-loader - that'll load the necessary script into the DOM. I added that to the
However, when I try to build this via
If I remove Any guidance would be much appreciated (and please do let me know if you think I'm barking up the wrong tree with this approach). |
Thanks for taking the time to figure this out! Regarding the typescript warning, you can't call a function ( You probably wish to implement a (class) |
Ah excellent, thank you, an explicit |
@smeijer I think I might be close to a solution, but I'm going to need some help to get it over the finish line. I've been working on a fork, https://github.com/darrenklein/leaflet-geosearch/tree/google-provider-fix - it's not working (nor compiling) in its current form, but I did kinda get it to work in a very limited way - but then I just pseudo-coded it as best I could to try to illustrate the flow that I think will work. Here's a bit of a summary as to what I've figured out so far, and what's up on my branch... In order to properly use Google's geocoding service when making dynamic geocoding requests from a web client, the page needs to import the Google Maps JS API. I was able to do this by adding the @googlemaps/js-api-loader NPM module, which is a pretty lightweight module that pulls that script in. That script is important, because it has a I set up googleProvider.ts to call the js-api-loader's The approach that I think that we want to take is to create a new instance of the Google Maps'
So yeah this isn't working code, but I think it's the idea. The final piece to the puzzle is proper configuration of the Google Maps API key - the key needs to be configured to allow both the Maps JavaScript API as well as the Geocoding API - Because the Geocoding API is enabled, you can still hit the original geocoding endpoint https://maps.googleapis.com/maps/api/geocode/json as well - however, the whole reason I ended up down this rabbit hole in the first place was because I wanted to be able to add a referrer restriction to my key, and the Geocoding API doesn't support that behavior. But good news! Once the key is enabled for the JavaScript API, you can safely add the referrer restriction - attempts to use the original geocoding endpoint will fail, because referrer restrictions aren't supported and the API just outright rejects the request, but an unauthorized client gets the message Anyway, sorry to dump a lot on you. Any guidance or feedback you can offer would be much appreciated! |
@smeijer I believe that I have a working fix for this - #311, but it's going to need some eyes on it. I mention this in the PR as well, but in my local dev environment, I can't quite seem to get this repo to work quite right - any time I geocode an address when using a local copy of this repo (regardless of the provider I'm using), I get errors in the calls to I'll try to dig into that some more, but what I can say so far is that this setup seems to work, and it looks like the data that we get back is being handled correctly. For example, the final result that we get from OSM looks like
and the result we get from the Google provider is
But definitely still work to do. Anyone available to give this a look over and advise? |
@darrenklein sorry for my absence. I've had a busy week. I'll do my best to catch up over the weekend. Thanks for all the hard work you've put into this. Much appreciated! |
@smeijer I've made some solid further progress with this, I'm just getting a little muddled with some of the types, and figuring out how best to handle testing. Would you be interested in maybe pairing with me sometime and seeing if we can knock this out together? I don't think it'd take too long. |
I think my PR might be just about where it needs to be - I did manage to figure out how to do some additional mocking for tests and got them to pass. Although I was hoping that this PR (#311) wouldn't introduce breaking changes, I'm afraid that it might have to, if it's to be done "right"... the challenge there is that, when the Google provider is instantiated, we need to make an async call to Google to load the Google Maps JS API into the DOM. Initially, I was making that call in the
Maybe there's a way to arrange this where we don't need the extra step? I'm not sure... but I think we want to be sure the script is loaded before trying to instantiate an object that would reference it. Well anyway, this is getting closer... I'd like to hear how you feel about this. I realize a breaking change is not ideal (especially one that means that the Google provider has a different end-user implementation than the other providers), but I do think it's an important step to take. |
Hello and thank you for your hard work! Are there any updates on this one or perhaps a roadmap on when we should (vaguely) expect it to be merged? |
This has been released via #311, thanks for all the hard work and collaboration here. Much appreciated ❤️ |
I have a Google Maps API key that's restricted to my domain, so that nobody can "steal" my key. Street View works, and other enabled APIs work, but geocoding using geosearch doesn't work. Switching to an unrestricted API key makes it work.
The text was updated successfully, but these errors were encountered: