Hello,
As HubSpot is going to remove the use of API keys I find myself in a situation to change all my scripts.
In this example, I pass the API key in the URL (url variable ), now with the OAuth Access Token I don’t know how and where to put it to be able to do my contact import.
Thanks for your help.
@dennisedson @ChehakWadhdwa @himanshurauthan @webdew
# .CV file / contacts to import
url = "https://api.hubapi.com/crm/v3/imports?hapikey=MYAPIKEY"
data = {
"name": "users_import", # import name
"files": [
{
"fileName": "previousdayusers.csv", # .csv filename
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "firstname",
"idColumnType": None,
"propertyName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "lastname",
"idColumnType": None,
"propertyName": "lastname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "email",
"idColumnType": "HUBSPOT_ALTERNATE_ID",
"propertyName": "email",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
}
]
}
}
]}
# preparing the JSON file / data
datastring = json.dumps(data)
payload = {"importRequest": datastring}
files = [('files', open('previousdayusers.csv','rb'))] # .csv filename
# pushing the contacts
response = requests.request("POST", url, data = payload, files = files)
print(response.text.encode('utf8'))
print(response.status_code)
Hi, @AbdelMOULIDA To clarify, your Private App key needs to be included in the header of your request and not as part of your request URL.
Here are some additional resources:
Have fun building! — Jaycee
Hello @Jaycee_Lewis ,
Thank you for your answer.
Yes, that’s what I understood but I don’t know how.
I put it in the request but it doesn’t work.
# .CV file / contacts to import
url = "https://api.hubapi.com/crm/v3/imports"
data = {
"name": "users_import", # import name
"files": [
{
"fileName": "previousdayusers.csv", # .csv filename
"fileFormat": "CSV",
"fileImportPage": {
"hasHeader": True,
"columnMappings": [
{
"ignored": False,
"columnName": "firstname",
"idColumnType": None,
"propertyName": "firstname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "lastname",
"idColumnType": None,
"propertyName": "lastname",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
},
{
"ignored": False,
"columnName": "email",
"idColumnType": "HUBSPOT_ALTERNATE_ID",
"propertyName": "email",
"foreignKeyType": None,
"columnObjectType": "CONTACT",
"associationIdentifierColumn": False
}
]
}
}
]}
# preparing the JSON file / data
datastring = json.dumps(data)
payload = {"importRequest": datastring}
files = [('files', open('previousdayusers.csv','rb'))] # .csv filename
# pushing the contacts
response = requests.request("POST", headers = {Authorization: Bearer {'MYTOKEN}}, data = payload, files = files)
print(response.text.encode('utf8'))
print(response.status_code)
tominal
November 16, 2022, 9:26pm
4
Hi @AbdelMOULIDA
From what I can see on the Python side, you should surround your Authorization Bearer token in a string:
r = requests.get('<MY_URI>', headers={'Authorization': 'Bearer: <MY_TOKEN>'})
Hope that helps!
Hi @tominal ,
Thank you for your answer.
I just tried your solution but it didn’t work.
What do you mean by ‘<MY_URI>’, how I can get it ?
Thank you.
tominal
November 21, 2022, 1:15am
6
The ‘<MY_URI>’ is referring to the API endpoint you are attempting to use. In this case I am guessing it is: https://api.hubapi.com/crm/v3/imports
Hope that helps!
Thank you @tominal for your answer.
I’m getting this new error when I’m executing the request:
Thanks for your help.
tominal
November 21, 2022, 5:35pm
8
Hey @AbdelMOULIDA ,
I would recommend that you take a look at the Private Apps documentation . You must specify certain scopes for your API requests before using them.
For this specific endpoint, you will need crm.import toggled on your Private App.
Hope that helps!
Thanks for your feedback and thanks again for your help @tominal .
I have activated all the scopes import, export … .
But I still have this access error :
Thanks for your help .
tominal
November 21, 2022, 9:53pm
10
There we go! Your API request is authenticated and is working now.
If you copy and pasted my code directly, then that was a GET request which returns a paged list of active imports for your account. You will want to change the “request.get” to “request.post” and adjust the syntax with the payload in your original post.
P.S. could you mark this post as the correct solution to your original question? This will help other developers on the forum searching for this exact issue.
Hi, @AbdelMOULIDA Let’s see if we can get the community to help out with some examples.
Hey, @taran42 @JBeatty , can you help @AbdelMOULIDA with an example using a Private App, Python, and the Imports API endpoints ?
Best,
Jaycee
Thank you @Jaycee_Lewis ,
But in Python please , my scripts are all in Python not in PHP.
Can’t wait to see some examples from you guys @tominal @Nickm66 @danhammari @JBeatty .