google_places
google_places copied to clipboard
How can I get next page results, getting error on spots_by_pagetoken method?
Hi, I am calling client.spots_by_pagetoken(pagetoken) method to get next 20 results but I am getting the error as Invalid Request Error? @marceldegraaf Can you please guide?
@mubashirkamran I'm no longer using/maintaining this gem. Maybe @qpowell can help? Cheers!
Hi @mubashirkamran - I've just tried running an example on my machine and it's working fine for me - do you mind giving me the query you're using to generate the next page token so I can try that specific request?
@edwinwills Here you go:
client = GooglePlaces::Client.new(ENV['google_places_api_key']) city = "Seattle" my_gyms = client.spots_by_query("Gym in #{city}", :types => ['gym']) next_page_token = my_gyms[-1].nextpagetoken # as the token is contained in last item next_page_gyms = client.spots_by_pagetoken(next_page_token) # this is the line that is causing the issue and i am using to get gyms on next page.
Hmm yeah - I can replicate that this end - looks like it's a difference between spots
and spots_by_query
- both return a valid looking next page token, but the spots_by_query
token never seems to work. Need to dig into this to see if maybe the response from Google is getting truncated/mangled somewhere.
Any update on this issue... I continue to look into it now, just didn't know if it was resolved and perhaps would save me some time. Thanks!
It appears there is a reference to this issue potentially here: https://github.com/qpowell/google_places/blob/master/lib/google_places/spot.rb#L395
I'm still looking into it, but when stepping through the document this seems to be where it errors out with the InvalidRequestError
Okay, figured it out I believe. You must put multipage: true, in your arguments attributes in the initial call of spots_by_query. So it should be preformed like so...
# get first 20
restaurants = @client.spots_by_query('restaurants in Baltimore', :types => ['restaurant', 'food'], multipage: true)
# process the restaurants acquired from google places api
process_restaurant_chunks(restaurants)
# while there is a next page token specified, keep processing
while restaurants.last.nextpagetoken.present?
restaurants = @client.spots_by_pagetoken(restaurants.last.nextpagetoken)
process_restaurant_chunks(restaurants)
end
Ah interesting - I wonder if it would be more helpful if the results always returned the correct nextpage token without needing the multipage
being set. I'll need to dig into the code a bit more to see why this is necessary, but it could well be for performance reasons.