我正在使用来自Google的Ruby自定义搜索引擎API。这是我在调用Google::APIClient.new
后设置API的方式:
response = client.execute(
:api_method => search.cse.list,
:application_name => 'my_app',
:application_version => '0.1',
:parameters => {
'q' => thekey,
'num' => 4,
'start' => 1,
'key' => 'MYKEY',
'cx' => 'MYCX'
}
)
请求消失了,但我无法达到下一页的结果。如果我运行client.execute(response.next_page)
获取第一个API调用的相同数据。
response.next_page
返回:
IRB(主):015:0> response.next_page => #“ordine dei geometri”,“num”=> 4,“start”=> 1,“key”=>“MYKEY”,“cx”=>“MYCX”,“pageToken”=> nil },@ header = {“User-Agent”=>“google-api-ruby-client / 0.7.1 Mac OS X / 10.9.4 \ n(gzip)”,“Accept-Encoding”=>“gzip “,”Content-Type“=>”“},@ api_method =#,@ authenticated = nil,@ authorization = nil,@ body
即使第一个start
返回
1
参数设置为response
"queries"=>{"nextPage"=>['start' => 5}
相反:
response.next_page_token
永远是nil
。
我尝试搜索(this not working for me),但有关Google API Ruby客户端的文档非常奇怪。
答案 0 :(得分:1)
以下是我在Google API调用中进行分页的模板:
page_token = nil
begin
parameters = {}
parameters['domain'] = "mydomain.org"
if page_token.to_s != ''
parameters['pageToken'] = page_token
end
result = $client.execute(
:api_method => api.groups.list,
:parameters => parameters)
if result.status == 200
groups = result.data
groups_array.concat(groups.groups)
page_token = groups.next_page_token
else
page_token = nil
end
end while page_token.to_s != ''
在此示例中,我检索特定域的所有组。
答案 1 :(得分:0)
好的,前一天我在google-api-ruby-client的github repo上打开了issue,这是最终答案:
自定义搜索使用非标准(与其他Google API相比)的方式来表示下一页。
next_page
期望在响应中找到属性next_page_token
并将其合并到上一个查询中。由于该值不存在,它只是重新执行上一个查询。
对于自定义搜索,您需要通过查看queries
中的next_page / previous_page角色来处理此问题。
所以,如果没有它,请执行:)