【发布时间】:2018-08-21 12:49:38
【问题描述】:
我正在尝试将 Hubspot API 中的所有交易都放入 Google 表格中。当交易计数低于 250 时,以下 Google App 脚本运行良好。现在我的管道中有超过 250 笔交易,我收到 429 个错误,“一天内调用服务太多次:urlfetch。”和其他错误.
function getDeals() {
// Prepare authentication to Hubspot
var service = getService();
var headers = {headers: {'Authorization': 'Bearer '+ service.getAccessToken()}};
// Prepare pagination
// Hubspot lets you take max 250 deals per request.
// We need to make multiple request until we get all the deals.
var keep_going = true;
var offset = 0;
var deals = Array();
while(keep_going) {
// We’ll take three properties from the deals: the source, the stage, the amount of the deal
var url = API_URL + "/deals/v1/deal/paged?&includeAssociations=true&properties=dealstage&properties=source&properties=amount&properties=dealname&properties=num_associated_contacts&limit=250&offset&properties=hubspot_owner_id&limit=250&offset="+offset;
var response = UrlFetchApp.fetch(url, headers);
var result = JSON.parse(response.getContentText());
Logger.log(result.deal)
// Are there any more results, should we stop the pagination
keep_going = result.hasMore;
offset = result.offset;
// For each deal, we take the stageId, source, amount, dealname, num_associated_contacts & hubspot_owner_id
result.deals.forEach(function(deal) {
var stageId = (deal.properties.hasOwnProperty("dealstage")) ? deal.properties.dealstage.value : "unknown";
var source = (deal.properties.hasOwnProperty("source")) ? deal.properties.source.value : "unknown";
var amount = (deal.properties.hasOwnProperty("amount")) ? deal.properties.amount.value : 0;
var dealname = (deal.properties.hasOwnProperty("dealname")) ? deal.properties.dealname.value : "unknown";
var hubspot_owner_id = (deal.properties.hasOwnProperty("hubspot_owner_id")) ? deal.properties.hubspot_owner_id.value : "unknown";
var num_associated_contacts = (deal.properties.hasOwnProperty("num_associated_contacts")) ? deal.properties.num_associated_contacts.value : "unknown";
deals.push([stageId,source,amount,dealname,num_associated_contacts,hubspot_owner_id]);
});
}
return deals;
}
【问题讨论】:
-
您多久打一次电话?这将控制
UrlFetchApp配额错误。此外,您的 URL 有两种使用offset- 一种在&limit=250之后,一种在末尾。这可能与您的 429 相关,即 HubSpot 的 API 可能采用参数的第一个值,因此偏移量始终为 0。您是否检查过在第二次调用时获得了不同的项目? -
您好,该脚本每 15 分钟触发一次,我认为这远低于 G Suite 用户的限制。我应该摆脱
offset的一种用途吗?
标签: google-apps-script hubspot hubspot-crm