Skip to content

Instantly share code, notes, and snippets.

@umair-khokhar
Last active July 24, 2019 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umair-khokhar/779bb1a7458f7f7baa4ec3cd3345912c to your computer and use it in GitHub Desktop.
Save umair-khokhar/779bb1a7458f7f7baa4ec3cd3345912c to your computer and use it in GitHub Desktop.
MantraHealth HubSpot Integration
# HubSpot Integration
The HubSpot integration consists of two components:
- HubSpot Developer App
- HubSpot library in the code base
## Installing the HubSpot Developer App
The HubSpot developer app uses Oauth 2.0 for installation. The installation is two part.
### Initiating the OAuth connection
Visit following URL:
https://app.hubspot.com/oauth/authorize?client_id=CLIENT_ID&scope=contacts%20automation%20timeline%20oauth&redirect_uri=https://www.domain.com/
Make sure the client_id and redirect_uri matches the credentials from the HubSpot app.
### Fetching Oauth access and refresh tokens
After successful initiation, the browser will be redirected to the redirect_uri with code in the query string. For example
https://www.domain.com?code=xxx-xxx-xxx-xxx
Use that code to make a curl request to fetch access and refresh tokens
curl -X POST --data "grant_type=authorization_code&client_id=CLIENT_ID&client_secret=CLIENT_SECRET&redirect_uri=https://www.domain.com/&code=xxx-xxx-xxx-xxx" https://api.hubapi.com/oauth/v1/token --header "application/x-www-form-urlencoded;charset=utf-8"
This request will return a response like this
{"refresh_token":"REFRESH_TOKEN","access_token":"ACCESS_TOKEN","expires_in":21600}
The acccess and refresh tokens should be stored in the redis server with following keys:
**hs_access_token** for access_token
**hs_refresh_token** for refresh_token
**hs_access_token_expiry** for expires_in
**hs_access_token_generation_time** for access token generation time, the token generation in seconds.
hs_access_generation_time is something that we need to generate ourselves. We can use following Javascript code
Math.floor(Date.now() / 1000)
### Regenerating the token after it has expired
libraries/crm/huspot.ts takes care of it automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment