使用其 API 从 Hubspot 检索交易

Posted

技术标签:

【中文标题】使用其 API 从 Hubspot 检索交易【英文标题】:Retrieve the deals from Hubspot using its APIs 【发布时间】:2021-11-30 08:11:10 【问题描述】:

我正在尝试将 Hubspot Deals 集成到 Google 电子表格中,我正在学习 https://medium.com/how-to-lean-startup/create-a-hubspot-custom-dashboard-with-google-spreadsheet-and-data-studio-27f9c08ade8d 的教程。但我收到错误“异常:数据中的列数与范围内的列数不匹配。数据有 0,但范围有 2。”。任何人都可以帮助我。谢谢。

var CLIENT_ID = '';     // Enter your Client ID
var CLIENT_SECRET = ''; // Enter your Client secret
var SCOPE = 'contacts';
var AUTH_URL = 'https://app.hubspot.com/oauth/authorize';
var TOKEN_URL = 'https://api.hubapi.com/oauth/v1/token';
var API_URL = 'https://api.hubapi.com';

function getService() 
   return OAuth2.createService('hubspot')
      .setTokenUrl(TOKEN_URL)
      .setAuthorizationBaseUrl(AUTH_URL)
      .setClientId(CLIENT_ID)
      .setClientSecret(CLIENT_SECRET)
      .setCallbackFunction('authCallback')
      .setPropertyStore(PropertiesService.getUserProperties())
      .setScope(SCOPE);

function authCallback(request) 
   var service = getService();
   var authorized = service.handleCallback(request);
   if (authorized) 
      return htmlService.createHtmlOutput('Success!');
    else 
      return HtmlService.createHtmlOutput('Denied.');
   


function authenticate() 
   var service = getService();
   if (service.hasAccess()) 
      // … whatever needs to be done here …
    else 
      var authorizationUrl = service.getAuthorizationUrl();
      Logger.log('Open the following URL and re-run the script: %s',authorizationUrl);
   


function getStages() 
  // Prepare authentication to Hubspot
  var service = getService();
  var headers = headers: 'Authorization': 'Bearer ' + service.getAccessToken();
  
  // API request
  var pipeline_id = "default"; // Enter your pipeline id here.
  var url = API_URL + "/crm-pipelines/v1/pipelines/deals";
  var response = UrlFetchApp.fetch(url, headers);
  var result = JSON.parse(response.getContentText());
  var stages = Array();
  
  // Looping through the different pipelines you might have in Hubspot
  result.results.forEach(function(item) 
    if (item.pipelineId == pipeline_id) 
      var result_stages = item.stages;
      // Let's sort the stages by displayOrder
      result_stages.sort(function(a,b) 
        return a.displayOrder-b.displayOrder;
      );
  
      // Let's put all the used stages (id & label) in an array
      result_stages.forEach(function(stage) 
        stages.push([stage.stageId,stage.label]);  
      );
    
  );
  
  return stages;



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?properties=dealstage&properties=source&properties=amount&limit=250&offset="+offset;
      var response = UrlFetchApp.fetch(url, headers);
      var result = JSON.parse(response.getContentText());
      // 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
      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;
         deals.push([stageId,source,amount]);
      );
   
   return deals;


var sheetNameStages = "Stages";
var sheetNameDeals = "Deals";
function writeStages(stages) 
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheetByName(sheetNameStages);
   // Let’s put some headers and add the stages to our table
   var matrix = Array(["StageID","Label"]);
   matrix = matrix.concat(stages);
   // Writing the table to the spreadsheet
   var range = sheet.getRange(1,1,matrix.length,matrix[0].length);
   range.setValues(matrix);

function writeDeals(deals) 
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheetByName(sheetNameDeals);
   // Let’s put some headers and add the deals to our table
   var matrix = Array(["StageID","Source", "Amount"]);
   matrix = matrix.concat(deals);
   // Writing the table to the spreadsheet
   var range = sheet.getRange(1,1,matrix.length,matrix[0].length);
   range.setValues(matrix);


function refresh() 
   var service = getService();
   if (service.hasAccess()) 
      var stages = getStages();
      writeStages(stages);
      var deals = getDeals();
      writeDeals(deals);
    else 
      var authorizationUrl = service.getAuthorizationUrl();
      Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
   

【问题讨论】:

除了复制和粘贴脚本之外,您还尝试过什么?当您尝试在数据和范围之间写入不匹配的列时,您遇到的错误实际上是 setValues 上的问题。看到它说“数据有 0 但范围有 2”意味着它需要 2 列,但从数据中收到 0。您有 2 个函数可以写入工作表,而需要 2 列的函数很可能是罪魁祸首,即writeStages。尝试使用setValues打印stagesmatrix,在写入前检查数据的有效性。 非常感谢您的回复,我可以做到的:)。 将我的评论重新发布为答案@MONICA。 【参考方案1】:

您在尝试写入时遇到的错误实际上是setValues 上的问题,并且数据和范围之间的列不匹配。

看到它说“数据有 0 但范围有 2”意味着它期望 2 列,但从数据中收到 0。

您有 2 个函数在工作表中使用 setValues,而需要 2 列的函数很可能是罪魁祸首 writeStages。尝试在使用 setValues 之前打印阶段和矩阵,以便在写入之前检查数据的有效性,然后您应该能够确定原因并解决问题。

【讨论】:

以上是关于使用其 API 从 Hubspot 检索交易的主要内容,如果未能解决你的问题,请参考以下文章

我们可以使用 active-merchant authorized.net COM API 从网关检索交易详情吗?

从信用卡跟踪交易数据,任何 API?

如何使用Python格式化Hubspot API的日期值

Hubspot 批量导入 API 限制问题

HubSpot company数据在UI上的展示和通过API方式进行获取

如何读取从 API 检索到的 JSON 并将其保存到 CSV 文件中?