Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Place Photo returns 403 when restricting access by referer

Steps:

  1. Create new Google API key
  2. View page with a Google Place Photo in an img tag
  3. Requested image is displayed
  4. Add 'HTTP referrer' restriction to key: https://example.com/*
  5. View page with a Google Place Photo in an img tag
  6. 403 response is returned instead of img

When using the same key for place autocomplete or for maps, everything still works fine. I would expect it to work fine with Place Photos as well, but it doesn't.

What might cause this issue?

like image 677
i_love_nachos Avatar asked Oct 18 '25 16:10

i_love_nachos


1 Answers

The place autocomplete is a part of Google Maps JavaScript API, so an API key with HTTP referrer restriction works well.

The Google place photo is a part of Places API web service. The web services support only IP address restrictions, they will fail with HTTP referrer restrictions on API keys.

You can read which restriction is applicable for each API here:

https://developers.google.com/maps/faq#keysystem

If your intention is using place photos in JavaScript code, you can just obtain a place from autocomplete using getPlace method as shown in this example and loop through photos array of PlaceResult object.

The google.maps.places.PlacePhoto object provides the method getUrl() that returns URL of the place photo. Use this method to get the URL of image.

Have a look at documentation for further details:

https://developers.google.com/maps/documentation/javascript/reference#PlacePhoto

place.photos.forEach(function (placePhoto) {
    var url = placePhoto.getUrl({
        maxWidth: 600,
        maxHeight: 400
    });
});
like image 65
xomena Avatar answered Oct 22 '25 08:10

xomena