2

我一直在使用 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 文档没有工作帖子示例的示例,因此我无法使用现有的任何内容来解决我的问题。在这一点上,我不确定我缺少什么,任何帮助或指导将不胜感激。

4

1 回答 1

2

我花了整整一周的时间与他们的内部支持人员讨论他们的文档中缺少的内容,但我终于让它工作了。

标题不应该{'accept': 'application/json'}像他们的大多数其他文档一样。相反,它需要{"importRequest": datastring}后跟数据对象的 jsonified 字符串。还必须创建一个文件对象,并允许读取二进制文件的绝对路径。在最后的 post 请求行中,它使用了带有 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)
于 2020-07-24T11:00:58.613 回答