使用Googleway包google_places()调用next page token获取第三页数据报错求助
解决Googleway获取第三页Places数据的报错问题
这个报错大概率是两个核心原因导致的,咱们一步步来排查解决:
1. 请求参数与初始请求不一致
你第三页的调用代码里缺少了place_type = "store"参数,但前两次请求都明确指定了这个参数。Google Places API有个严格要求:使用page_token时,后续请求的所有参数(除了page_token本身)必须和第一次请求完全一致,否则API会返回无效响应,进而导致googleway解析JSON时失败。
修正后的基础调用代码
# 前两页保持原有逻辑不变 key <- 'insert own api key' loc <- c(33.685494, -117.812070) df <- google_places(place_type = "store", location = loc, radius = 1500, key = key) df_next <- google_places(place_type = "store", location = loc, radius = 1500, key = key, page_token = df$next_page_token) # 第三页必须保留所有初始参数,仅替换page_token df_next_two <- google_places(place_type = "store", location = loc, radius = 1500, key = key, page_token = df_next$next_page_token)
2. Next Page Token存在生效延迟
Google Places API的next_page_token并不是生成后立刻可用的——服务器需要一点时间生成后续页面的数据集,如果你在获取第二页后立刻调用第三页,token可能还未生效,返回的不是标准JSON响应,从而触发解析错误。
添加延迟+重试机制
在调用第三页前添加几秒延迟,必要时加上重试逻辑,确保token完全生效:
# 获取前两页后,等待3秒让token完成初始化 Sys.sleep(3) # 用tryCatch处理可能的重试场景 tryCatch({ df_next_two <- google_places(place_type = "store", location = loc, radius = 1500, key = key, page_token = df_next$next_page_token) }, error = function(e) { message("首次调用失败,再等待2秒后重试...") Sys.sleep(2) df_next_two <- google_places(place_type = "store", location = loc, radius = 1500, key = key, page_token = df_next$next_page_token) })
额外检查项
- 确认你的API密钥已经启用了Places API,并且有足够的请求配额;
- 检查
df_next$next_page_token是否不为空,确保确实存在第三页数据(如果搜索结果不足60条,token会是空值)。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

