APIs & Integrations

AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

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)

 

 

0 Votes
1 Solution acceptée
tominal
Solution
Guide | Partenaire solutions
Guide | Partenaire solutions

How to change the URL that contains the API key by OAuth?

Résolue

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.


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com

Voir la solution dans l'envoi d'origine

0 Votes
12 Réponses
Jaycee_Lewis
Gestionnaire de communauté
Gestionnaire de communauté

How to change the URL that contains the API key by OAuth?

Résolue

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

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Votes
AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

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 🙂  .

0 Votes
Jaycee_Lewis
Gestionnaire de communauté
Gestionnaire de communauté

How to change the URL that contains the API key by OAuth?

Résolue

Updated my post

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Votes
Jaycee_Lewis
Gestionnaire de communauté
Gestionnaire de communauté

How to change the URL that contains the API key by OAuth?

Résolue

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

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Votes
AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

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)

 

0 Votes
tominal
Guide | Partenaire solutions
Guide | Partenaire solutions

How to change the URL that contains the API key by OAuth?

Résolue

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!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Votes
AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

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.

0 Votes
tominal
Guide | Partenaire solutions
Guide | Partenaire solutions

How to change the URL that contains the API key by OAuth?

Résolue

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!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Votes
AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

Thank you @tominal for your answer.

 

I'm getting this new error when I'm executing the request:

AbdelMOULIDA_0-1669022064325.png

Thanks for your help.

0 Votes
tominal
Guide | Partenaire solutions
Guide | Partenaire solutions

How to change the URL that contains the API key by OAuth?

Résolue

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!


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Votes
AbdelMOULIDA
Contributeur

How to change the URL that contains the API key by OAuth?

Résolue

Thanks for your feedback and thanks again for your help @tominal .

 

I have activated all the scopes import, export ... .

Screenshot 2022-11-21 at 22.35.58.png

 

But I still have this access error 😞

Screenshot 2022-11-21 at 22.33.37.png

 Thanks for your help 🙂.

0 Votes
tominal
Solution
Guide | Partenaire solutions
Guide | Partenaire solutions

How to change the URL that contains the API key by OAuth?

Résolue

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.


Thomas Johnson
Community Champion


Kahu Software LLC
A Texan HubSpot consulting firm
https://kahusoftware.com
0 Votes