0

我正在尝试通过以下 URI 获取我所有的 4,809 家公司:

CreateCompURI = "https://api.hubapi.com/companies/v2/companies/paged?hapikey=" + DestinationAPIKey + "&limit=250&properties=account_number";

一旦打了第一个电话,我就会得到 ahasmore bool和 an offset value

"has-more": true,
    "offset": 8018991168

然后我在下一个 URI 中使用它:

CurrentCompanyURI = "https://api.hubapi.com/companies/v2/companies/paged?hapikey=" + DestinationAPIKey + "&offset=" + offset + "&limit=250&properties=account_number";

hasmore bool但是,在使用和 对while 循环进行多次迭代后offset,循环是无限的并且列表不断增长。

当没有更多公司时,布尔值应该返回 false GET。但是,布尔值保持为真。

这是我在 C# 上使用的方法:

public List<HubSpotCurrentComnpany> GetCurrentCompanies()
{
    List<HubSpotCurrentComnpany> companies = new List<HubSpotCurrentComnpany>();
    bool hasmore = false;

    try
    {
        do
        {
            using (var client = new HttpClient())
            {
                if (offset != 0)
                {
                    CurrentCompanyURI = CurrentCompanyURI.Replace("offset=0", "offset=" + offset);
                    client.BaseAddress = new Uri(CurrentCompanyURI);
                }
                else
                {
                    client.BaseAddress = new Uri(CreateCompURI);

                }

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AccessToken);

                var response = client.GetAsync("").Result;
                var readTask = response.Content.ReadAsStringAsync().Result;
                Company_ResponseModel result =
                    JsonConvert.DeserializeObject<Company_ResponseModel>(readTask,
                    new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                foreach (var company in result.companies)
                {
                    companies.Add(new HubSpotCurrentComnpany()
                    {
                        companyids = Convert.ToString(company.companyId),
                        name = company.properties?.name?.value,
                        account_number = company.properties?.account_number?.value
                    });

                }
                if (result.HasMore)
                {
                    hasmore = true;
                    offset = result.offset;
                }
                else
                {
                    hasmore = false;
                    offset = 0;
                }
            }
        } while (hasmore);
        return companies;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

我怎样才能得到我所有的 4,809 家公司?我究竟做错了什么?

4

0 回答 0