【问题标题】:How does pagination works on Zoho CRM API?分页如何在 Zoho CRM API 上工作?
【发布时间】:2021-04-01 05:27:17
【问题描述】:

我正在从 Zoho CRM API 获取数据

import requests
import json  
import pandas  

refresh_token="xxxx"  
client_id="xxxx"
client_secret="xxx"  

req1=requests.post("https://accounts.zoho.com/oauth/v2/token?refresh_token=" + refresh_token + "&client_id="+ client_id + "&client_secret=" + client_secret + "&grant_type=refresh_token")
print(req1.status_code)  
data=req1.json()  
acc_token=data['access_token']  

headers= {'Authorization': 'Zoho-oauthtoken '+acc_token}
url="https://www.zohoapis.com/crm/v2/Leads"  
r= requests.get(url,headers=headers)  
print(r.status_code)  
print(r.json()) 

我收到了回复,但我只能获得 200 条记录。 JSON 响应末尾有以下详细信息

'info': {'per_page': 200, 'count': 200, 'page': 1, 'more_records': True}}.

请帮我获取所有记录

【问题讨论】:

    标签: python api python-requests jupyter-notebook zoho


    【解决方案1】:

    根据 zoho 文档,您可以提供 page=per_page= URL 参数来选择要阅读的页面,以及需要多少个结果。

    这是您正在调用的端点的文档: https://www.zoho.com/crm/developer/docs/api/v2/get-records.html

    及相关参数:

    sort_order(字符串,可选)对任意一个中的记录列表进行排序 升序或降序。可能的值:asc - 升序; desc - 降序

    sort_by(字符串,可选)根据字段指定API名称 必须对哪些记录进行排序。可能的值:字段 API 名称。 示例:电子邮件

    page(整数,可选)从 相应的页面。 page 的默认值为 1。可能的值: 仅限正整数值。

    per_page(整数,可选)获取每个可用的记录列表 页。页的默认值为 200。可能的值:正整数 仅值。

    致电https://www.zohoapis.com/crm/v2/Leads?page=2 应该会给您接下来的 200 个结果

    【讨论】:

    • 如何获取输出中的所有记录。如何合并所有页面并获得输出@Eloims
    【解决方案2】:

    在这里,您将使用 pagination,因为您必须编写类似这样的代码。

    more_records = True
    page_number = 1
    
    while more_records:
        get_url="https://www.zohoapis.com/crm/v2/Leads?page="+str(page_number)
        r = requests.get(url=get_url, headers=headers)
    
        for lead in r:
          """YOUR CODE"""
    
        page_number += 1
        more_records = r.json()['more_records']
    

    【讨论】:

    • 我能够获取 200 条记录,但出现 KeyError: 'access_token'。
    • 铅是什么意思?
    • 您将在一页中获得 200 条记录,然后再次使用 page=2 访问相同的 url,然后其他记录将出现然后您将能够获取它们......while循环用于分页直到more_records 是真的,for 循环是一个接一个地获取所有即将到来的记录....领先意味着 r 中的单个记录
    猜你喜欢
    • 2020-03-26
    • 2019-05-25
    • 2019-11-04
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 2014-04-04
    • 1970-01-01
    相关资源
    最近更新 更多