OAuth token expires so user has to manually refresh access token. How to do that automatically?

I have a question in regards to the OAuth process. Here is my scenario:
I have a webhook app that I am hosting/running outside of Hubspot. This webhook receives payloads of data that then uses the Hubspot API to add contacts to the user’s Hubspot account (I have also created a Hubspot public app).
Here is the user flow:

  1. The user goes to the URL of the webhook (for demonstration purposes let’s say this URL is https://webhook.com).
  2. Once they have gone to that URL they are prompted to use OAuth to authorize their Hubspot account.
  3. Once the user has authorized my Hubspot app they are able to send payloads to my webhook’s endpoint (again, just for demonstration purposes here) https://webhook.com/endpoint.
  4. My webhook receives that payload and then updates the user’s Hubspot account by adding contacts to it.
    This works; however my question is in regards to the OAuth portion. The OAuth token expires after a specific amount of time thus the user has to manually refresh it in order to have the webhook continue working. This is the issue I’m having.
    How can I set my app up so that the user only has to authenticate with OAuth once? As this is a webhook I want it to be able to run in the background indefinitely after the user has given access to it (well until the delete/remove my Hubspot app from their account).
    Thanks for your help!

Hi @ayeteejay :waving_hand:

You’ll likely need to store the relevant OAuth credentials somewhere

(e.g. database/datastore)

that can be securely accessed by your app/web-service. As per HubSpot’s OAuth documentation and their OAuth Quickstart Guide, in order to successfully authenticate a HubSpot API request

(via OAuth)

, you must exchange your app credentials and the relevent refresh token

(specific to unique combinations of [1] a HubSpot user within [2] a HubSpot account)

for a new, temporarily valid access token.

Broadly speaking, the simplest way to achieve this is:

  1. Capture and store information at the time someone successfully installs your app. This information might include a unique identifier for the user

(e.g. email address)

and/or HubSpot account

(e.g. HubSpot Portal ID)

, alongside the relevant app refresh token.

  1. When a user then tries to take an action relevant to your app/webhook, your app will need a way to identify who’s taking the action

(e.g. account log in functionality or simple identification via submission of email address / HubSpot Portal ID)

.

  1. Retrieve the relevant stored refresh token from your database/datastore

(from step 1)

and exchange it for a new access token

(as described above).

I hope this proves useful. Please let me know if you have any follow-up questions.