This is my first time working with the Google Places API. I'm doing an exercise in Python where I'm trying to get all stores in a 20km radius from a set point.
I'm able to get the first 20 results without problems, but I'm not sure how to get the next 20. From what I read in the documentation, I need to use "next_page_token", but I haven't been able to do so successfully.
Here's my test code:
url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.4196301,-99.16895&radius=20000&type=store&key=" + api_key
response = requests.get(url).json()
if response and response.get("status") == "REQUEST_DENIED":
print(response)
sys.exit(1)
elif response and response.get("status") != "ZERO_RESULTS":
print(json.dumps(response, indent=4, sort_keys=True))
print(len(response["results"]))
print(">>>> PAGE 1")
for result in response["results"]:
print("Store name: ", result["name"])
if "next_page_token" in response:
print(">>>> PAGE 2")
page2_token = response["next_page_token"]
url_page_2 = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=19.4196301,-99.16895&radius=20000&type=store&pagetoken=" + page2_token + "&key=" + api_key
print(url_page_2)
response_page_2 = requests.get(url_page_2).json()
print(response_page_2)
for result in response_page_2["results"]:
print("Store name: ", result["name"])
else:
print("no next page")
When my code tries to execute the request for the 2nd page, I keep getting this error message:
{'html_attributions': [], 'results': [], 'status': 'INVALID_REQUEST'}
I'm not sure what I'm doing wrong when formatting the 2nd page request (or even I'm doing it correctly). Any ideas?