因此,我尝试使用 Python requests.post 对此 Kartra Inbound API 进行 API post 调用。这是我的 Python 代码:
payload = {
'api_key': settings.KARTRA_API_KEY,
'api_password': settings.KARTRA_API_PASSWORD,
'app_id': settings.KARTRA_APP_ID,
'lead': {
'email': '[email protected]',
'first_name': 'Joseph',
'last_name': 'Smith'
},
'actions': {
'0': {'cmd': 'search_lead'}
}
}
url = settings.KARTRA_API_URL
response = requests.post(url, data=payload)
我必须遵循的唯一示例是 PHP(我没有使用过)。事情是这样的:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(
array(
'app_id' => 'AIm863DwsOW',
'api_key' => 'QG9GPLW8G',
'api_password' => 'kdwFAfwrfVS',
'lead' => array(
'id' => '3232323223', //you may pass either ID or EMAIL. If both, the system will pick ID
'email' => '[email protected]',
),
'actions' => array(
'0' => array(
'cmd' => 'search_lead',
),
)
)
) );
我所能得到的只是这样的错误:
{"status":"Error","message":"'actions' not an array","type":"224"}
我已验证我的密钥和令牌似乎工作正常。
我已经尝试了所有我能想到的方法来让端点将“动作”解释为数组。有人可以帮我纠正一下吗?非常感谢,因为我实际上浪费了几个小时来搞乱这个。
正如我在评论中所说,Kartra 支持没有帮助。 Soo...我拿了 PHP 示例并将其发送到 https://httpbin.org/post 以了解实际发送的内容。这本身就是一个启示(从来不知道这样的工具)。
这里是 PHP 示例的链接:https://documentation.kartra.com/section-28-php-sample-creating-a-lead/
以下是实际发送到 api 的内容:
{
"args": {},
"data": "",
"files": {},
"form": {
"actions[0][cmd]": "create_lead",
"actions[1][cmd]": "assign_tag",
"actions[1][tag_name]": "My customer",
"api_key": "QG9GPLW8G",
"api_password": "kdwFAfwrfVS",
"app_id": "AIm863DwsOW",
"lead[custom_fields][0][field_identifier]": "text1",
"lead[custom_fields][0][field_value]": "text message",
"lead[custom_fields][1][field_identifier]": "dropdown1",
"lead[custom_fields][1][field_value]": "612",
"lead[custom_fields][2][field_identifier]": "checkbox1",
"lead[custom_fields][2][field_value][0]": "620",
"lead[custom_fields][2][field_value][1]": "621",
"lead[email]": "[email protected]",
"lead[first_name]": "Joe",
"lead[last_name]": "Smith"
},
"headers": {
"Accept": "*/*",
"Content-Length": "678",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-5f77604a-7b5fee841e85d78f6b352133"
},
"json": null,
"origin": "104.14.117.45",
"url": "https://httpbin.org/post"
}
这是用于测试的实际工作 Python 代码:
payload = {
'api_key': settings.KARTRA_API_KEY,
'api_password': settings.KARTRA_API_PASSWORD,
'app_id': settings.KARTRA_APP_ID,
'lead[email]': '[email protected]',
'lead[first_name]': 'Joe',
'lead[last_name]': 'Smith',
'actions[0][cmd]': 'create_lead',
}
url = settings.KARTRA_API_URL
response = requests.post(url, data=payload)
我从中学到的另一点是 requests.post() 库不会在 Python 字典的字典的第二层以下进行深层复制。有关更多信息,请参阅我原来的问题...
我真心希望这可以帮助别人避免我为解决这个问题而经历的痛苦。诚然,如果我对 Python、HTTP 和 PHP 了解更多,并且没有试图避免站起来 PHP 来测试示例代码,事情会更容易。活到老,学到老!
这个 PHP 代码:
'actions' => array(
'0' => array(
'cmd' => 'search_lead',
),
可以解释为
list
或dict
,你可以尝试将PHP位“翻译”为:
'actions': [
{
'cmd': 'search_lead'
}
]
编辑:。 如果你尝试一下会怎样:
'actions': [
{
0: {'cmd': 'search_lead'}
}
]
如果错误像
'actions' not an array
那么简单,那么这只意味着一件事 - actions aren't given as an array
。在 Python 中,数组用 []
方括号括起来,大括号 {}
用于 dictionary
数据结构。
简单地将操作部分更改为数组应该可以解决问题
'actions': [
{
0: {'cmd': 'search_lead'}
}
]
详细的 API 还解释了错误的原因这里。
Kartra 的 API 给我带来了很多麻烦,但这里有一个有效的 Python 脚本:
import requests
payload = {
'api_key': API_KEY,
'api_password': API_PW,
'app_id': API_ID,
'lead[email]': '[email protected]',
'lead[first_name]': 'Joe',
'lead[last_name]': 'Smith',
'actions[0][cmd]': 'create_lead',
}
url = 'https://app.kartra.com/api'
response = requests.post(url, data=payload)
print(response)
print(response.text)
我无法在 Postman 中使用它,直到我设置了 内容类型:x-www-form-urlencoded
其格式如本答案所述: https://stackoverflow.com/a/4073451/15195663
因此,RAW 帖子正文实际上最终看起来像这样(这在 Postman 中按原样添加了内容类型标题):
app_id=API_ID&api_key=API_KEY&api_password=API_PW&lead%5Bemail%5D=TESTTESTTEST%40me.com&lead%5Bfirst_name%5D=Joe&lead%5Blast_name%5D=Smith&actions%5B0%5D%5Bcmd%5D=create_lead
仅供参考,如果有人尝试获取 GoHighLevel 的 webhook 来调用 Kartra 来创建用户,则您不能这样做,因为 GHL 只按原样接受 JSON。尝试通过 Zapier 代替。