Google static map API getting 403 forbidden when l

2020-02-26 14:47发布

问题:

What I have is a Google map that shows the location of a property but when I come to print the dynamic maps dont print so good so I decided to implement the Google Static Map image API.

http://lpoc.co.uk/properties-for-sale/property/oldgate-dairy-st-james-road-long-sutton-cambridgeshire-pe12/?prop-print=1

^^ is an example of a property in print view and should show a static map image but it fails to load and looking at my inspector I'm getting a 403 Forbiden response for the image.

But if I go to the URL directly the image loads...

What am I doing wrong?

Thanks

Scott

回答1:

This has gotten quite a lot of views, so I'm adding my solution to the problem here:

When using the new API, make sure you generate a Key for browser apps (with referers) and also make sure the patterns match your URL.

E.g. when requesting from example.com your pattern should be

  example.com/*

When you're requesting from www.example.com:

  *.example.com/*

So make sure you check whether a subdomain is present and allow both patterns in the developer console.

  1. Visit the Developer Console.
  2. Under API Keys, click the pencil icon to edit.
  3. Under "Key restrictions", ensure that you have an entry for example.com/*, *.example.com/*, and any local testing domains you might want.


回答2:

There seems to be some confusion here, and since this thread is highly ranked on Google, it seems relevant to clarify.

Google has a couple of different API's to use for their maps service:

Javascript API

The old version of this API was version 2, which required a key. This version is deprecated, and it is recommended to upgrade to the newer version 3. Note that the documentation still states that you need a key for this to function, except if you're using "Google Maps API for Business".

Static Maps API

This is a whole different story. Static maps is a service that does not require any javascript. You simply call an url, and Google will return a maps image, making it possible to insert the URL directly into your <img> tag. The newest version is version 2, and this requires a key to function because a usage limit is applied.

A key can be requested here: https://code.google.com/apis/console

And the key should be added to the request for the correct image to be generated:

http://maps.googleapis.com/maps/api/staticmap?center=New+York,NY&zoom=13&size=600x300&key=API_console_key

I hope this clears up some confusion.



回答3:

I had this same problem but my solution was different. I had the V2 maps api enabled, but not the static maps api (I thought this was V2). I enabled the static maps api and it worked.



回答4:

Oops I feel like such an idiot. I was using the old V2 maps API URL and not the new V3 API URL. I was getting a 403 because I was using the V2 URL without providing an API key :(



回答5:

Be hundred percent sure of these points: (for static maps)

  1. Enable your project at this url :

https://console.developers.google.com/apis/api/static_maps_backend/overview?project=

  1. You have your localhost, staging and production - all urls with wildcards enabled in the referrer section.

  2. Google has changed its policy and you now need an api key to display maps. refer this for more : Google Maps API without key?

Hope it helps.



回答6:

Staticmaps V3 doesn't need the "Key" attribute and removing it seems to solve the <img> source problem.

Try with an URL like this:

http://maps.googleapis.com/maps/api/staticmap?center=0.0000,0.0000&zoom=13&size=200x200&maptype=roadmap&markers=0.0000,0.0000&sensor=false

For more information read this.



回答7:

Yeah, Google Maps API version 3 were java-script version; "Google Static Maps" latest were 2.0. I suspect there might be some restriction on use.



回答8:

I could also not display static maps and could see 403 error in the browser's network console.

http response headers:

status:403
x-content-type-options:nosniff

I had an API key with a lot of Google Maps APIs enabled but the Google Static Maps API was missing, enabling it solved the issue.



回答9:

now you should use 'signature' parameter, which you should add to request - otherwise static maps won't work. here is few useful links

1) how to generate signature

2) how to make signature on BE side (code snippet)



回答10:

I am using Wordpress 4.9.4 with ChurchThemes Exodus Theme. I had applied for & generated a New API_KEY.

  • I confirmed it was being used when calling the map: Google Map Link
  • However the Js Console showed the following error: Google Maps Error in Js Console
  • As Johnny White mentioned above I had to navigate to the API Library Screen via APIs & Services Menu: enter image description here
  • You will be greeted by the API Library screen: API Library Screen
  • Click on Maps(17) Lower LHS.
  • Search for & click Google Static Maps API - Enable it if needed: Google Static Maps API
  • You may also need to enable Google Maps Javascript API (same process as for Static Maps: Google Maps Javascript API

Once that is done your maps should start appearing on your site or app.

If they don't appear on refresh you may need to:

  • clear your cache (Wordpress or Drupal webistes),
  • wait the 5 min recommended for the API to Register the enabled API's