0

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?

Alain
  • 339
  • 3
  • 19
  • 1
    Duplicate of [Paging on Google Places API returns status INVALID\_REQUEST](https://stackoverflow.com/questions/21265756/paging-on-google-places-api-returns-status-invalid-request) – MrUpsidown May 18 '21 at 22:36

2 Answers2

1

Just found the solution! When you get the next page token, you need to wait a couple of seconds before getting the next page. Somehow the token info isn't yet in google servers if you request right away. Adding a simple time.sleep(2) between requests solved this.

Alain
  • 339
  • 3
  • 19
0

For next page results try setting only the pagetoken parameter in the url. Remove all other parameters.

pagetoken — Returns up to 20 results from a previously run search. Setting a pagetoken parameter will execute a search with the same parameters used previously — all parameters other than pagetoken will be ignored.

Reshab Das
  • 185
  • 7
  • This is correct, it works... however it doesn't solve the issue. I still get the same error. – Alain May 17 '21 at 20:32
  • I just tried it out. Used only `pagetoken` and `key` parameters. Try it out, it worked for me. Used same parameters as yours. Also try checking the url. – Reshab Das May 17 '21 at 20:58