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

使用LangChain Agent调用HubSpot API时认证错误求助

问题:APIChain调用HubSpot API创建联系人时认证失败,Postman可正常运行

代码示例

HubSpotDocs =  """

The below is Hubspot API documentation showing how to create a contact. 

The endpoint is https://api.hubapi.com/crm/v3/properties/contacts. 

///Example request body to create a new contact
{
  "properties": {
    "email": "example@hubspot.com",
    "firstname": "Jane",
    "lastname": "Doe",
    "phone": "(555) 555-5555",
    "company": "HubSpot",
    "website": "hubspot.com",
    "lifecyclestage": "marketingqualifiedlead"
  }
}
 """

key="KEY"

# Set the headers
headers = {
    'Authorization': f'Bearer {key}',
    'Content-Type': 'application/json'
}


llm = ChatOpenAI(temperature=0, model= 'gpt-3.5-turbo-1106', openai_api_key="KEY")

Test = APIChain.from_llm_and_api_docs(llm, HubSpotDocs,limit_to_domains=None, verbose=True)

Test.run("Create a new entry named Sara")

问题描述

尝试通过Agent连接HubSpot API创建联系人,运行上述代码时返回认证错误:

'The API call to create a new contact named Sara returned an error message stating that authentication credentials were not found. The API supports OAuth 2.0 authentication, and more details can be found at the provided link.'

但使用相同的授权令牌和端点在Postman中可正常执行请求,怀疑是授权格式或代码配置问题,寻求解决帮助。

原因分析与解决方案

1. 端点地址错误(核心问题)

你提供的HubSpotDocs里的端点是创建联系人属性的接口,并非创建联系人的接口:

  • 错误端点:https://api.hubapi.com/crm/v3/properties/contacts
  • 正确创建联系人端点:https://api.hubapi.com/crm/v3/objects/contacts

Postman中你应该使用的是正确端点,而代码里LLM会依据错误文档调用错误接口,进而引发认证或功能异常。

2. APIChain未使用自定义Headers

你手动定义了带授权信息的headers,但APIChain.from_llm_and_api_docs默认不会自动读取该变量。LLM会根据你提供的HubSpotDocs生成请求,而文档未提及Authorization头的要求,导致生成的请求缺失认证信息,最终报错。

修复步骤

步骤1:修正HubSpot文档中的端点与认证说明

更新HubSpotDocs,替换为正确端点并补充认证要求:

HubSpotDocs =  """
The below is Hubspot API documentation showing how to create a contact. 

The endpoint is POST https://api.hubapi.com/crm/v3/objects/contacts.

Authentication requires an Authorization header with format: Bearer {your-api-key}
Content-Type must be application/json.

///Example request body to create a new contact
{
  "properties": {
    "email": "example@hubspot.com",
    "firstname": "Jane",
    "lastname": "Doe",
    "phone": "(555) 555-5555",
    "company": "HubSpot",
    "website": "hubspot.com",
    "lifecyclestage": "marketingqualifiedlead"
  }
}
 """

步骤2:让APIChain加载自定义Headers

初始化APIChain时,通过request_kwargs参数传递Headers:

from langchain.chains.api import APIChain

# 替换为你的真实HubSpot API密钥
key = "YOUR_REAL_HUBSPOT_API_KEY"
headers = {
    'Authorization': f'Bearer {key}',
    'Content-Type': 'application/json'
}

Test = APIChain.from_llm_and_api_docs(
    llm, 
    HubSpotDocs,
    limit_to_domains=None, 
    verbose=True,
    request_kwargs={"headers": headers}  # 传递认证Headers
)

步骤3:替换占位符密钥

确保代码中的key和openai_api_key都替换为真实密钥,不要保留占位符"KEY"。

验证

修改后运行代码,LLM会依据正确端点并携带认证头发起请求,即可解决认证错误问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:15:32