googlemaps / googlemaps/google-maps-services-python

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

未關閉
#366 6 則留言 5 個 reaction 已指派 0 人 在 GitHub 檢視
triage me type: bug
主要語言
Python
星號
5k
分支
1.4k
PR 合併指標
30 天內沒有已合併 PR

描述

#### 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
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)
```

貢獻指南

開啟貢獻指南

研究方向

Read googlemaps/places.py around places and _places, then googlemaps/client.py around _request and _get_body. Reproduce the pagination request with and without the reported two-second delay; done means determining whether the client behavior is responsible and recording the expected handling of the INVALID_REQUEST response.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
api
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。