新手小白,如何对接hubspot crm系统

49 阅读2分钟

HubSpot OAuth 2.0 接入指南:从授权到获取联系人列表

前言

HubSpot 是一款流行的CRM平台,提供了丰富的API接口。本文将详细介绍如何通过OAuth 2.0授权流程接入HubSpot,并获取联系人列表数据。

准备工作

  1. 在HubSpot开发者平台创建应用:
    • 访问 应用管理页面
    • 记录以下凭证:
      client_id = 3ad80484-xxxx-xxxx-xxxx-738c1b0c846c
      client_secret = a939a514-xxxx-xxxx-xxxx-d96d79983e93
      

OAuth 2.0 授权流程

1. 构建授权URL

用户需要通过以下URL进行授权:

https://app.hubspot.com/oauth/authorize?
  client_id=3ad80484-xxxx-xxxx-xxxx-738c1b0c846c&
  scope=crm.export%20crm.lists.read%20crm.objects.companies.read%20crm.objects.contacts.read%20crm.objects.courses.read%20oauth&
  redirect_uri=http://localhost

注意:scope参数定义了应用请求的权限范围

2. 获取授权码

授权成功后,用户将被重定向到回调地址,URL中会包含授权码:

http://localhost/?code=na1-fe12-xxxx-xxxx-xxxx-b92b89efafa8

3. 换取访问令牌

使用授权码向HubSpot的令牌端点发起请求:

请求端点

POST https://api.hubapi.com/oauth/v1/token

请求参数

grant_type=authorization_code&
client_id=3ad80484-xxxx-xxxx-xxxx-738c1b0c846c&
client_secret=a939a514-xxxx-xxxx-xxxx-d96d79983e93&
redirect_uri=http%3A%2F%2Flocalhost&
code=na1-f5a8-xxxx-xxxx-xxxx-af1d514073de

成功响应

{
    "token_type": "bearer",
    "refresh_token": "na1-4c36-xxxx-xxxx-xxxx-eb52154b95cb",
    "access_token": "CNyI4dC7MhIeAAEAUAAAAAAAAAAIAAAAAACAAAAAAAAAAAAAAAAEGMbNkRcgoYvzIyi7iNACMhR15iLfFvgXFkB3UbhXCgfK1t4AbTpgAAAAQQAAAAAAAAAAAAAAAACGAAAAAAAAAAAAAAAAAAAAYCAAAAAAAAAcAAAAACACAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPQhQRUT55V9vuGgQKfBrhErNwuQ_9VkoDbmExUgBaAGAA",
    "expires_in": 1800
}

获取联系人列表

获得访问令牌后,可以调用CRM API获取联系人数据:

请求端点

GET https://api.hubapi.com/crm/v3/objects/contacts?limit=10&properties=email&properties=firstname&properties=phone

请求头

Authorization: Bearer [你的access_token]

注意事项

  1. 访问令牌有效期较短(1800秒),需要使用refresh_token刷新
  2. 生产环境请勿使用localhost作为redirect_uri
  3. 请妥善保管client_secret,不要泄露

参考文档


希望这篇指南能帮助你顺利接入HubSpot API!如果有任何问题,欢迎在评论区留言讨论。