We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Nov 9, 2022 3:09 AM - edited Nov 9, 2022 4:53 AM
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)
Solved! Go to Solution.
Nov 21, 2022 4:53 PM
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.
Nov 15, 2022 11:56 AM - edited Nov 15, 2022 12:25 PM
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
Nov 15, 2022 12:05 PM
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 🙂 .
Nov 15, 2022 12:25 PM
Nov 9, 2022 1:11 PM
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
Nov 9, 2022 4:08 PM - edited Nov 9, 2022 4:09 PM
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)
Nov 16, 2022 4:26 PM
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!
Nov 17, 2022 3:46 AM - edited Nov 17, 2022 3:51 AM
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.
Nov 20, 2022 8:15 PM
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!
Nov 21, 2022 4:14 AM
Thank you @tominal for your answer.
I'm getting this new error when I'm executing the request:
Thanks for your help.
Nov 21, 2022 12:35 PM
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!
Nov 21, 2022 4:38 PM
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 🙂.
Nov 21, 2022 4:53 PM
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.