首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用API令牌通过动态循环和Excon主机提取所有JSON页面结果

使用API令牌通过动态循环和Excon主机提取所有JSON页面结果
EN

Stack Overflow用户
提问于 2017-07-24 02:53:15
回答 1查看 324关注 0票数 0

我希望使用令牌(通过动态循环)提取所有JSON客户结果页。默认情况下,它只显示从第一页获得的结果。代码如附件所示。请建议一下。

代码语言:javascript
复制
# Request authorization via MainDB using Company ID
response = Excon.post(
  "https://MainDB_APIEndpoint/#{Customer_Id}/login",
  headers: { 'Authorization' => API_TOKEN }
)

# Get the login URL from the response
login_url = JSON.parse(response.body)['login_url']

# Make a request to the MainDB API for that tenant to auth with that tenant
response = Excon.get(login_url)

# Extract the cookies which will be used to auth future MainDB API requests for this tenant
cookies = response.data[:cookies].join('; ')

#Uses the cookie to make API calls to that customer 
response = Excon.get(
"https://MainDB_APIEndpoint/sales/tickets",
 headers: { 'Cookie' => cookies })

#Parses and returns result from the first page
page_data = JSON.parse(response.body)

csv = CSV.open('./output1_ticket.csv', 'w')
headers = nil
page_data['results'].each do |result|
    if headers.nil?
        headers = result.keys
        csv << headers
    end
    csv << headers.map { |key| result[key] }
end

#Tried getting all the JSON pages but this doesn't seem to work
pages = Excon.get(
"https:/MainDB_APIEndpoint/sales/tickets/?page=5",
query: { page: page })
EN

回答 1

Stack Overflow用户

发布于 2017-08-02 02:11:47

尝试使用以下语法:

代码语言:javascript
复制
connection = Excon.new('https:/MainDB_APIEndpoint/sales/tickets')
connection.get(query: { page: page })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45271927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档