APIs & Integrations

AMeilleurecop
Participant

Trouble connecting with OAuth with Python client

Hi there,

 

I am currently trying to simply connect to my app with the python client using OAuth, so I've read the doc and the GitHub repo but cannot figure out how to authenticate and get an access token.

 

from hubspot.auth.oauth import ApiException

try:
    tokens = api_client.auth.oauth.tokens_api.create_token(
        grant_type="authorization_code",
        redirect_uri='http://localhost',
        client_id='client_id',
        client_secret='client_secret',
        code='code'
    )
except ApiException as e:
    print("Exception when calling create_token method: %s\n" % e)

With this sample, you get the access token, but to do that you need the code passed as parameter that according to the doc, you need to call the authorize url first to get a code.

To my understanding you need to call the URL, then get the code after being redirected, but I don't see a way to do that with the python client.

 

After that, I am trying to push a contact using this

new_contact = SimplePublicObjectInput(properties=data)
  try:
      res = hubspot_client.crm.contacts.basic_api.create(simple_public_object_input=new_contact)
  except ApiException as e:
      logger.error(f'Failed to create contact in HubSpot: {e}')

Is this also a good way to do things ?

 

I feel like either I am missing something obvious or the doc is misleading.

Can anyone help me figure this out ?

I would really appreciate the help,

Matthieu

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

Trouble connecting with OAuth with Python client

Hi, @AMeilleurecop 👋 Thanks for reaching out. Hey, @tominal @JBeatty do yhou have any tips you can share with @AMeilleurecop?

 

Best,

Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Votes
AMeilleurecop
Participant

Trouble connecting with OAuth with Python client

Hi and thanks for your response,

I managed to do it by simply not using the python library to get the temporary code. I'm just getting it manually through a browser and using the refresh token generated to get the token when I need to.

I honestly find it not optimal as I would have prefered to do it programmatically through and through, but seeing as the doc does not help in that regard, it will have to do 🙂

0 Votes