在 NodeJS 中,我使用的是“@hubspot/api-client”:“^7.1.2”。
使用accessToken创建hubspot客户端如下
const hubSpotClient = new hubspot.Client({ accessToken });
当我尝试使用电子邮件更新联系人时,它抛出错误
要求:
const idProperty = 'email';
const response = await hubSpotClient(store).crm.contacts.basicApi.update(email, idProperty, contact);
回复:
ERROR {
"statusCode": 404,
"body": {
"status": "error",
"message": "Object not found. objectId are usually numeric.",
"correlationId": "71e911d3...",
"context": {
"id": [
"testemail@..."
]
},
"category": "OBJECT_NOT_FOUND"
}
创建联系人与该客户工作正常,但通过电子邮件更新不起作用。 传递 idProperty 时有任何不合适的地方或语法错误吗?
问题出在您的实现中,因为您似乎没有正确使用 Hubspot API。
如果您检查basicApi.update
public async update(contactId: string, simplePublicObjectInput: SimplePublicObjectInput, idProperty?: string, _options?: Configuration): Promise<RequestContext> {
基本上,您需要传递一个 contactId,然后传递一个
simplePublicObjectInput
,它 基本上是一个对象,代表您的更新。
您的代码应如下所示:
import { Client } from "@hubspot/api-client";
const hubspotClient = new Client({ accessToken: YOUR_ACCESS_TOKEN });
const contactID = 1234;
const response = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: '[email protected]' },
})
请记住,Hubspot 始终尝试遵循与其端点相同的准则。如果您检查端点规范,您将看到以下内容:
将 Hubspot 节点客户端视为某些 http 客户端的抽象,但最终的作用与其实现中描述的端点完全相同。
因此,在您的实现中,Hubspot 返回一个适当的错误,因为您没有在第一个参数中提供 contactId,Hubspot 会告诉您:“找不到对象。objectId 通常是数字。”因为确实联系人 ID 是数字,而您正在使用电子邮件的值 --string-- 来代替。
如果您想“通过电子邮件更新” 我认为没有直接的方法可以做到这一点,您可能需要通过电子邮件预先搜索联系人。 您可以使用
searchApi
。
获取 id 后只需运行更新即可。
const searchResponse = await hubspotClient.crm.contacts.searchApi.doSearch({
filterGroups: [
{
filters: [
{
value: '[email protected]',
propertyName: 'email',
operator: 'EQ',
},
],
},
],
sorts: [],
properties: [],
limit: 1,
after: 0,
});
// Off course you need to improve a lot the error handling here and so on.
// This is just an example
const [contactID] = searchResponse.results;
const contactUpdateResponse = await hubspotClient.crm.contacts.basicApi.update(contactID, {
properties: { email: '[email protected]' },
})
希望对您有帮助!
您可以使用电子邮件作为 hubspot/api-client 获取联系功能的 idProperty,但只有在 idProperty 之前填写所有其他查询字段时它才有效,即使它们未定义。
这是我在 Node 中将 getContactByEmail 作为 Google Cloud 函数的示例,使用 api-client,效果非常好!
exports.getContactByEmail = functions.https.onCall(async (data, context) => {
const email = data.email;
const contactId = email;
const properties = ["firstname", "lastname", "company"];
const propertiesWithHistory = undefined;
const associations = undefined;
const archived = false;
const idProperty = "email";
try {
const apiResponse = await hubspotClient.crm.contacts.basicApi.getById(
contactId,
properties,
propertiesWithHistory,
associations,
archived,
idProperty
);
console.log(JSON.stringify(apiResponse.body, null, 2));
return apiResponse.properties;
} catch (error) {
error.message === "HTTP request failed"
? console.error(JSON.stringify(error.response, null, 2))
: console.error(error);
return error;
}
});
我认为您可以在不使用
/search
端点获取 contactID 的情况下执行此操作,并且可以使用电子邮件作为 idProperty
您只需要告诉 Hubspot 您正在使用电子邮件作为请求中的 id .
PATCH
:https://api.hubapi.com/crm/v3/objects/contacts/:email?idProperty=email
{
"properties": {
"propertyName": "newPropertyValue"
}
}
此 Hubspot 用户论坛帖子对于弄清楚其工作原理很有帮助。
他们暗示你可以在文档中做到这一点,但他们没有明确说明如何做。
对于现有联系人,电子邮件和记录 ID 都是唯一值,因此您可以使用 id 或电子邮件通过 API 更新联系人。
要通过联系人 ID 更新单个联系人,请向 /crm/v3/objects/contacts/{contactId} 发出 PATCH 请求,并包含您要更新的数据。
?idProperty
似乎是正确的方法。