2
votes

After hours of searching and trying every possible solution found on the web, I cannot try anything else for my problem, so I really need help:

I want to implement a simple autocomplete text box with geocode results, so I call AJAX requests while the user types in the text box

  • I have enabled Google Maps and Google Places from the Google APIs Console
  • I have created a Key for server apps with my server's IP
  • I have a PHP file (called through AJAX) running on the server which sends the request to Google Places using file_get_contents() function (SSL is enabled) - also tried with cURL function
  • The request I'm asking for is

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=MY_SEARCH_STRING&language=us&types=geocode&sensor=false&key=MY_KEY

where MY_SEARCH_STRING is a simple string like "London" and MY_KEY the Key I have created

  • what I get as a response is

{ "predictions" : [], "status" : "REQUEST_DENIED" }

  • I have also tried this with a browser key. Also tried to create a new project, enable Services all over again, and create new Key. Switched services on/off & creating new key many times in any combination. Nothing worked.

  • The strange thing is that the same code was working the last months in a landing page I had created on the server, but had a long time to check it, so I do not know when it stopped working.

I appreciate a lot any help!! Thank you.

3
The above request works fine with my API Key. Have you considered using the Places Library of the Google Maps JavaScript API v3? - Chris Green
I manage my textbox with jQuery autocomplete, so I want to declare a php script that takes the query string, runs on the server, and returns the result to jQuery autocomplete UI. So, I don't want to implement any client-side solution (which works by the way, while the server-side doesn't). - Alex Christodoulou
UPDATE: I've created a simple PHP file which implements a simple search request using the Places API, exactly copied from Google Places API documentation. It still gets REQUEST_DENIED. I think that for some reason my key gets rejected. I've double checked that the IP allowed for that key is the same with my server's IP. - Alex Christodoulou

3 Answers

5
votes

SOLVED. In Google API Console, I had declared allowed server IPs. I was assuming that if I declared an IP, I was just ensuring that requests would be allowed only from that IP. In order for this to work, you must also declare the allowed per-user limits for each IP, or else it just doesn't allow any requests. I removed all the allowed IPs, waited for 3-4 minutes, and the request was allowed.

1
votes

I've had a similar issue, fixed it and discovered a few things that may be useful in troubleshooting this

  • using http instead of https will result in REQUEST_DENIED
  • omitting &userIp=x.x.x.x will result in REQUEST_DENIED
  • a server key needs to be generated and used in case the request is from a php script even if ONLY consumed via browser by users or it will result in REQUEST_DENIED
  • a few minutes are necessary before testing if the list of IPs allowed has been changed

Hope it helps some of you guys