Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination in Places Produces ApiError: INVALID_REQUEST (Rate-limit Problem?) #366

Open
ChicagoDev opened this issue May 30, 2020 · 6 comments · May be fixed by #462
Open

Pagination in Places Produces ApiError: INVALID_REQUEST (Rate-limit Problem?) #366

ChicagoDev opened this issue May 30, 2020 · 6 comments · May be fixed by #462
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@ChicagoDev
Copy link

Environment Details

API Used: Places
OS: Mac OS 10.15.4 (Catalina)
Python Version: 3.8
Library Version: 4.4.1
Other Info: Executed in PyCharm

Steps to Reproduce

  1. Retrieve a list of places with a radius
  2. submit a next page request with next_page_token

Code that Produces Exception

import googlemaps
import json
from pprint import pprint
import time


## Import API Key
with open('config.json') as config_file:
    config_data = json.load(config_file)

## Establish API Connection
gmaps = googlemaps.Client(key=config_data['maps_api_key'])

## Find places
barber_shops = gmaps.places('barber shop', radius=3218)

#Extract Next Page Token
npt = barber_shops["next_page_token"]

#For Usability
print(f'The first business returned is {barber_shops["results"][0]["name"]}')
print(f'\t The placed id is {barber_shops["results"][0]["id"]}')
print(f'\n\t\tThe next_page_token is {npt}')

#Request the next page || Raises googlemaps.exceptions.ApiError: INVALID_REQUEST
next_page_barber_shops = gmaps.places('barber shop', radius=3218, page_token=npt)
pprint(next_page_barber_shops)

Stack Trace

Traceback (most recent call last):
  File "/Users/bjg/CDA/GMaps_LeadGen/rate_limit_bug", line 26, in <module>
    next_page_barber_shops = gmaps.places('barber shop', radius=3218, page_token=npt)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googlemaps/client.py", line 418, in wrapper
    result = func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googlemaps/places.py", line 243, in places
    return _places(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googlemaps/places.py", line 409, in _places
    return client._request(url, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googlemaps/client.py", line 313, in _request
    result = self._get_body(response)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/googlemaps/client.py", line 342, in _get_body
    raise googlemaps.exceptions.ApiError(api_status,
googlemaps.exceptions.ApiError: INVALID_REQUEST

Extra

Adding time.sleep(2) resolves this error and the code executes

Working Code
import googlemaps
import json
from pprint import pprint
import time


## Import API Key
with open('config.json') as config_file:
    config_data = json.load(config_file)

## Establish API Connection
gmaps = googlemaps.Client(key=config_data['maps_api_key'])

## Find places
barber_shops = gmaps.places('barber shop', radius=3218)

#Extract Next Page Token
npt = barber_shops["next_page_token"]

#For Usability
print(f'The first business returned is {barber_shops["results"][0]["name"]}')
print(f'\t The placed id is {barber_shops["results"][0]["id"]}')
print(f'\n\t\tThe next_page_token is {npt}')

#Request the next page || Raises googlemaps.exceptions.ApiError: INVALID_REQUEST
#next_page_barber_shops = gmaps.places('barber shop', radius=3218, page_token=npt)
#pprint(next_page_barber_shops)

#However if time.sleep(2) is utilized, there is no error:
time.sleep(2)
next_page_barber_shops = gmaps.places('barber shop', radius=3218, page_token=npt)
pprint(next_page_barber_shops)
@ChicagoDev ChicagoDev added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels May 30, 2020
@jpoehnelt
Copy link
Contributor

I haven't had a chance to look into this. Did you find anything else on this?

@Giuzzilla
Copy link

Giuzzilla commented Jul 13, 2020

Having the same issue, and can confirm that time.sleep(2) solves it, so it seems to be a rate-limiting issue.

If I call the API twice with no page_token the request works correctly.
If I query multiple different "type" with no sleep I don't get any rate-limiting/error either.

The problem is there even if you don't use a radius.

@skumarlabs
Copy link

skumarlabs commented Aug 30, 2020

Had the same problem, fixed it using time.sleep(2)
The reason for this behavior is also mentioned in the documentation

There is a short delay between when a next_page_token is issued, and when it will become valid. Requesting the next page before it is available will return an INVALID_REQUEST response. Retrying the request with the same next_page_token will return the next page of results.

@therahulmehta
Copy link

There is a short delay between when a next_page_token is issued, and when it will become valid. Requesting the next page before it is available will return an INVALID_REQUEST response. Retrying the request with the same next_page_token will return the next page of results.

@skumarlabs could you point me to where you found that? I just can't seem to find that in the docs now.

@zkne zkne linked a pull request Dec 7, 2022 that will close this issue
4 tasks
@jpoehnelt jpoehnelt removed their assignment Jan 18, 2023
@koosha-t
Copy link

koosha-t commented Oct 2, 2023

Any updates on this?

@Tylerlhess
Copy link

This should either be closed as working as designed (as it is according to the docs) or add in an automatic sleep timer with override option when using a next_page_token.

Or better yet update the library to use the Nearby Search (New) api endpoint which only returns 20 results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants