rapigoogle-apinullgoogleway

Next page token R Google Places API


I am attempting to get restaurant details from the Google places API in R. I can successfully get the 20 results that Google provides however I realize that as it returns a next_page_token, then this means there are other results (which is logical is there are more than 20 restaurants around the 50km radius of the location I am searching). However when I use next_page_token it returns NULL. Here is my code:

install.packages("googleway")
library(googleway)

key <- 'my key'

df_places <- google_places(search_string = "restaurant", 
                       location = c(-33, 151),
                       page_token = "next_page_token",
                       radius = 50000,
                       key = key)
df_places$results$name
df_places$results$rating
df_places$results$formatted_address
df_places$results$geometry$location

And here is what it returns without next_page_token:

 [1] "Gennaro's Italian Restaurant"                   
 [2] "Mount Broke Wines & Restaurant"                 
 [3] "Oak Dairy Bar"                                  
 [4] "Bimbadgen"                                      
 [5] "Subway Restaurant"                              
 [6] "Muse Restaurant"                                
 [7] "Ocean Restaurant"                               

etc...

Here is what it returns with next_page_token:

> df_places$results$name
NULL

Just wondering what I am doing wrong to repeatedly return NULL?


Solution

  • First thing to check, are you using the quoted string "next_page_token", or an object next_page_token that you get from the result to the first query to google_places()

    Here's a working example of your code

    library(googleway)
    
    key <- 'api_key'
    
    df_places <- google_places(search_string = "restaurant", 
                               location = c(-33, 151),
                               radius = 50000,
                               key = key)
    
    token <- df_places$next_page_token
    
    df_next_places <- google_places(search_string = "restaurant", 
                               location = c(-33, 151),
                               page_token = token,
                               radius = 50000,
                               key = key)
    
    df_places$results$name[1:5]
    # [1] "Gennaro's Italian Restaurant"   "Mount Broke Wines & Restaurant"
    # [3] "Oak Dairy Bar"                  "Bimbadgen"                     
    # [5] "Subway Restaurant"
    
    df_next_places$results$name[1:5]
    # [1] "RidgeView Restaurant"          "Emerson's Cafe and Restaurant"
    # [3] "EXP. Restaurant"               "Margan Restaurant & Winery"   
    # [5] "AL-Oi Thai Restaurant"