我一直在使用Hubspot文档来尝试批量上传具有Hubspots CRM API的名字,年龄,电话号码,城市和站点URL的联系人。我已经尝试过同时处理一个csv文件和一个具有五行测试数据的xlsx文件(在对它们进行测试时,都将fileFormat更改为CSV和SPREADSHEET)。我的文件与调用它的python程序位于同一目录中,所以我知道路径不是问题。
这是我的python代码:
import requests
import json
post_url = 'http://api.hubapi.com/crm/v3/imports?hapikey=c76....901'
latest_file = "thisIsTestData.csv"
headers = {'accept': 'application/json'}
data = {
"name": "import_contacts",
"files": [
{
"fileName": latest_file,
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "FirstName",
"idColumnType": None,
"propertName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "Web Site URL",
"idColumnType": None,
"propertyName": "website",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "Ad_Age",
"idColumnType": None,
"propertyName": "ad_age",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "City",
"idColumnType": None,
"propertyName": "city",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "Mobile Phone Number",
"idColumnType": None,
"propertyName": "mobilephone",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
]
}
}
]
}
r = requests.post(url=post_url, data=data, headers=headers)
print(r.status_code)
print(r.text)
我在底部添加了一个status_code打印,并收到415响应。我检查了Hubspot,但没有任何测试值已上传,因此我知道某些操作绝对无效。该文件位于正确的位置,我使用帐户设置来验证列映射是否正确命名并与csv和xlsx中的现有列名称匹配,api密钥正确,并且我已经获得了请求集发布。不幸的是,hubspot文档没有工作示例的示例,因此我无法使用任何现有的方法来解决问题。我现在还不确定我缺少什么,任何帮助或指导都将不胜感激。
答案 0 :(得分:2)
花了整整一周的时间,与他们的内部支持人员讨论了他们文档中缺少的内容,但是我终于使它工作了。
标题应该不像其他大多数文档中的{'accept': 'application/json'}
那样。相反,它需要{"importRequest": datastring}
,后跟数据对象的JSON字符串。还必须创建一个文件对象,并允许以二进制形式读取文件的绝对路径。在最后的发帖行中,它使用了带有api键的精心制作的url,JSON化的数据字符串,以及最终对csv文件的二进制读取。
您不想转移的csv中的任何列值都必须标记为"ignored":True,
,而不是从数据配置中省略。数据必须按照它们在csv文件中出现的顺序列出。
这是我更新的代码,它反映了许多需要的更改。
import os
import requests
import json
url = "http://api.hubapi.com/crm/v3/imports?hapikey={insert HAPI KEY HERE}"
full_path = os.path.abspath("TestData.csv")
data = {
"name": "test_import",
"files": [
{
"fileName": "TestData.csv",
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "Web Site URL",
"idColumnType": None,
"propertyName": "website",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "FirstName",
"idColumnType": None,
"propertyName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "Ad_Age",
"idColumnType": None,
"propertyName": "ad_age",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "Mobile Phone Number",
"idColumnType": None,
"propertyName": "mobilephone",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": False,
"columnName": "City",
"idColumnType": None,
"propertyName": "city",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifiedColumn": False
},
{
"ignored": True,
"columnName": "Create Date"
},
{
"ignored": True,
"columnName": "Stock Photo"
}
]
}
}
]}
datastring = json.dumps(data)
payload = {"importRequest": datastring}
files = [
('files', open(full_path, 'rb'))
]
response = requests.request("POST", url, data=payload, files=files)
#Output checking
print(response.text.encode('utf8'))
print(response.status_code)