简体   繁体   English

Hubspot 批量导入 CRM 数据 API 问题

[英]Hubspot Bulk Import CRM Data API question

I've been using the hubspot documentation to attempt a batch upload contacts that have a first name, age, phone number, city, and site url with hubspots CRM API .我一直在使用 hubspot 文档尝试使用 hubspots CRM API批量上传具有名字、年龄、电话号码、城市和站点 url 的联系人。 I've tried with both a csv file, and an xlsx file with five rows of test data (I'd changed fileFormat to CSV and SPREADSHEET when testing them both).我已经尝试过使用 csv 文件和带有五行测试数据的 xlsx 文件(我在测试它们时将 fileFormat 更改为 CSV 和 SPREADSHEET)。 I have the file sitting in the same directory as my python program that calls it, so I know pathing is not an issue.我的文件与调用它的 python 程序位于同一目录中,所以我知道路径不是问题。

Here's my python code:这是我的 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)

I've added a status_code print at the bottom, and receive a 415 response.我在底部添加了一个 status_code 打印,并收到 415 响应。 I checked hubspot, and none of the test values have been uploaded so I know something is definitely not working.我检查了 hubspot,并没有上传任何测试值,所以我知道某些东西肯定是行不通的。 The file is in the correct location, I've used the account settings to verify that the column mappings are correctly named and match the existing column names in the csv and xlsx, the api key is correct, and I've got the request set to POST.该文件位于正确的位置,我已使用帐户设置来验证列映射是否正确命名并与 csv 和 xlsx 中的现有列名匹配,api 密钥是否正确,并且我已获得请求集发布。 Unfortunately the hubspot docs do not have an example of a working post example, so I can't use anything existing to troubleshoot my issues.不幸的是,hubspot 文档没有工作帖子示例的示例,因此我无法使用现有的任何内容来解决我的问题。 I'm not sure at this point what it is I'm missing, and any help or guidance would be appreciated.在这一点上,我不确定我缺少什么,任何帮助或指导将不胜感激。

It took all week of grinding away and talking with their internal support about what was missing from their documentation, but I finally got it working.我花了整整一周的时间与他们的内部支持人员讨论他们的文档中缺少的内容,但我终于让它工作了。

The header should not have been {'accept': 'application/json'} like in most of their other documentation. header 不应该像大多数其他文档那样是{'accept': 'application/json'} Instead it required {"importRequest": datastring} followed by a jsonified string of the data object.相反,它需要{"importRequest": datastring}后跟数据 object 的 jsonified 字符串。 A files object also had to be created, and allowed to read the absolute path to the file in binary.还必须创建一个文件 object,并允许读取二进制文件的绝对路径。 In the final post request line, it used the crafted url with api key, the jsonified data string, and finally the binary reading of the csv file.在最后的 post 请求行中,它使用了精心制作的 url 和 api 键,json 化的数据字符串,最后是 csv 文件的二进制读取。

Any column value in the csv that you did not want transferred over, had to be marked with "ignored":True, instead of omitted from the data configuration. csv 中您不想转移的任何列值都必须标记为"ignored":True,而不是从数据配置中省略。 The data had to be listed in the same order they appear in the csv file as well.数据也必须按照它们在 csv 文件中出现的顺序列出。

Here's my updated code that reflects a lot of the changes that were needed.这是我更新的代码,它反映了许多所需的更改。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM