I have this code in my MVC Controller:

[OutputCache(VaryByParam = "*", Duration = 3600, Location =  System.Web.UI.OutputCacheLocation.ServerAndClient)]
public async Task<string> GetAPI(string callback, string libraries)
    var location_selector_script = System.IO.File.ReadAllText(Server.MapPath("~/Scripts/nav/locselector.js"));
    var res = await Utilities.API.GetAsync("https://maps.googleapis.com/maps/api/js?key=" + ConfigurationsHelper.GoogleMapsAPIKey
                + "&callback=" + callback + "&libraries=" + libraries);
            return res + location_selector_script;

and call it like this:

<script async defer src="/Navigation/GetAPI?callback=initMap&libraries=places"></script>

Is this a good idea to initialize Google APIs in backend?

  • \$\begingroup\$ That looks like it hides your API key from the front-end? Even if it's not foolproof, it sounds like a great idea in general. \$\endgroup\$ – CertainPerformance Nov 6 '20 at 19:13
  • \$\begingroup\$ @CertainPerformance Yes that's why I was going for this approach. But just a minute ago I found that it doesn't hides the API key because when I moved the marker on the map, the frontend sends some requests with my API key to the Google API through browser. I saw them in the console. But I want know if I use this approach will it effect the SSL source that is required by the Google API endpoint? \$\endgroup\$ – Aishwarya Shiva Nov 6 '20 at 19:25
  • \$\begingroup\$ I this may be more of a question for stack overflow. \$\endgroup\$ – pacmaninbw Nov 6 '20 at 22:22
  • \$\begingroup\$ @AishwaryaShiva Well, if you need to hide the request traffic, you may need to register a HttpClientHandler with a different WebProxy to the HttpClient then use it. This would make the request uses a different proxy than the pre-configured one, which hides the traffic from the current connection for basic tracing tools such as browsers and Fiddler or similar tools. \$\endgroup\$ – iSR5 Nov 7 '20 at 9:29
  • \$\begingroup\$ @iSR5 Can you give me an example? \$\endgroup\$ – Aishwarya Shiva Nov 8 '20 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.