You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pipedrive API获取Leads时遭遇404错误求助

解决Pipedrive API 404错误:获取指定筛选器Leads列表

错误原因分析

你遇到的404错误提示Unknown method,核心问题出在Pipedrive API请求的有效性上,常见诱因包括:

  • API令牌未正确定义或为空
  • Leads模块未在你的Pipedrive账户中启用
  • 筛选器ID无效或无权限访问
  • API请求的细节(如URL格式)存在问题

具体修复步骤

1. 补全API令牌定义

你的代码中直接使用api_token变量但未声明赋值,这会导致请求URL中的令牌参数为空,引发API解析错误。在函数开头添加令牌定义:

var api_token = "你的Pipedrive API令牌"; // 替换为你实际的API令牌

2. 验证Leads模块状态

登录Pipedrive后台,确认Leads功能已启用:

  • 进入「设置」→「功能」页面
  • 找到Leads模块,确保其处于开启状态(未开启的话无法调用Leads相关API)

3. 确认筛选器ID有效性

  • 登录Pipedrive,找到ID为603的筛选器,检查它是否属于Leads筛选器类型
  • 确保该筛选器未被删除,且你的API令牌拥有访问该筛选器的权限

4. 添加调试选项排查请求问题

给UrlFetchApp.fetch添加muteHttpExceptions选项,获取完整的服务器响应,便于精准定位问题:

var response = UrlFetchApp.fetch("https://api.pipedrive.com/v1/leads?filter_id=" + filter_id + "&api_token=" + api_token, {muteHttpExceptions: true});
Logger.log(response.getContentText()); // 打印完整响应内容到日志

5. 修复代码中的其他潜在问题

除了404错误,你的代码还有几个易引发报错的点:

  • 所有变量添加var声明,避免全局变量污染(比如custom_fields、annual_volumes等)
  • 处理联系人手机号为空的情况,避免数组越界报错:
var phone_num = person.phone && person.phone.length > 0 ? person.phone[0].value : "unknown";
  • 修正phone_num = leads[i].phone_num的错误,原Lead对象没有该属性,应直接使用之前获取的phone_num变量

修正后的完整代码示例

function fetchLeads() {
  var api_token = "你的Pipedrive API令牌"; // 替换为实际令牌
  var custom_fields = {
      'Annual Volume':'a34088d0a923af5494c5f3ce114cb4b2f3c40b85',
      'Platform': '0be41c69233227d207bb44530185c7ef9a7f8b97',
      'Application ID': '9f2a22d43753f142be8d6f07dc40978393b81b06',
      'Channel':'4e105d77d6d4f99413f8434dfb781ceb3eb77999',
      'CandidateStage':'b90ecc9f2633d78bad3102a085150f06a7009510'
  };

  var annual_volumes = {
      132: 'less_than_1m',
      133: 'from_1m_to_5m',
      134: 'from_5m_to_15m',
      135: 'more_than_15m'
  };

  var filter_id = 603;
  var leads_final = [];

  var requestUrl = "https://api.pipedrive.com/v1/leads?filter_id=" + filter_id + "&api_token=" + api_token;
  Logger.log(requestUrl);

  // 获取未分配Leads,添加异常处理
  var response = UrlFetchApp.fetch(requestUrl, {muteHttpExceptions: true});
  var resp = JSON.parse(response.getContentText());
  
  if (!resp.status) {
    Logger.log("API请求错误: " + resp.error);
    return leads_final;
  }

  var leads = resp.data || [];
  Logger.log("获取到Leads数量: " + leads.length);

  for (var i=0; i<leads.length; i++ ) {
    var lead = leads[i];
    if (!lead.person_id) {
      Logger.log("Lead " + lead.id +  "无关联联系人,跳过");
      continue;
    }

    // 获取关联联系人信息
    var personResponse = UrlFetchApp.fetch("https://api.pipedrive.com/v1/persons/" + lead.person_id + "?api_token=" + api_token, {muteHttpExceptions: true});
    var personResp = JSON.parse(personResponse.getContentText());
    
    if (!personResp.status || !personResp.data) {
      Logger.log("获取联系人" + lead.person_id + "失败: " + personResp.error);
      lead.country = "unknown";
    } else {
      var person = personResp.data;
      var phone_num = person.phone && person.phone.length > 0 ? person.phone[0].value : "unknown";

      // 根据手机号推断国家
      if (phone_num.startsWith('+971') || phone_num.startsWith('971')) {
        lead.country = 'emirates';
      } else if (phone_num.startsWith('+966') || phone_num.startsWith('966')) {
        lead.country = 'saudi';
      } else if (phone_num.startsWith('+20') || phone_num.startsWith('20')) {
        lead.country = 'egypt';
      } else if (phone_num.startsWith('+965') || phone_num.startsWith('965')) {
        lead.country = 'kuwait';
      } else if (phone_num.startsWith('+974') || phone_num.startsWith('974')) {
        lead.country = 'qatar';
      } else {
        lead.country = 'unknown';
      }
    }

    // 整理Leads数据
    var lead_id = lead.id;
    var lead_title = lead.title;
    var annual_volume = annual_volumes[lead[custom_fields['Annual Volume']]] || "unknown";
    var platform = lead[custom_fields['Platform']] || "unknown";
    var country = lead.country;
    var channel = lead[custom_fields['Channel']] || "unknown";
    var candidatestage = lead[custom_fields['CandidateStage']] || "unknown";

    leads_final.push([lead_id, lead_title, annual_volume, platform, country, channel, candidatestage]);
  }

  // 打印调试信息
  for (var i=0;i<leads_final.length;i++ ) {
    console.log(leads_final[i][6]);
  }
  return leads_final;
}

内容的提问来源于stack exchange,提问作者Altaf Kalpatt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 11:17:32