By the end of this blog, you will know enough about how to integrate Hubspot with your web application and query hubspot apis using the Hubspot OAuth in Nodejs on AWS Lambda.
Prerequisites
You may need some exposure & access to the following in order to understand the intent of this blog.
- Access to some Hubspot Instance
- NodeJs
- AWS Lambda
Let’s get started
Create an app using Hubspot Developer Account
To do OAuth, you must create a hubspot app. We do this to get the client Id and client secret to initiate the oauth flow and integrate with the hubspot apis upon successful authentication and receive the access token.
- Login into your Hubspot developer account
- Create app by providing all the necessary details
- Select the created app -> Basic info –> Auth. Note down the client Id and client secret
- Set the Callback URL to: https://localhost/token. This is the URL Hubspot will redirect to upon successful validation of user credentials, along with the authorization code. You would need authorization code to get the access token. Don’t worry about the details yet, it is explained in detail as we go along
Interaction sequence of various components involved
Dissection of sequence diagram
Connect to Hubspot OAuth2.0 endpoint
1) When you open your browser to http://localhost:3000/, the application will invoke the /login api(in aws lambda) which redirects(as a result of 302 response from api) the user to the authentication page on HubSpot’s server.
2) User will choose the appropriate signin method and login into the hubspot account.
3) After successful login, hubspot will take the user to the page to choose the account the user would like to install the app(in hubspot) in and give consent for it to act on your behalf.
4a) When the user grants access to the app with the listed permissions, HubSpot will redirect the user back to the application. This is decided based on the redirectUrl configured while creating the app(in hubspot)
4b) When the user denies the consent, hubspot will take the user to the hubspot home page
Retrieve the authorization code
5) When hubspot redirects the user back to the application, it supplies the authorization code in the query parameters(decided based on the redirectUrl configured while creating the app in hubspot)
Retrieve the access token
Once you’re back in the application, invoke /token api(in aws lambda) which will retrieve the access token and refresh token from HubSpot’s server(by calling hubspot client library nodejs api - https://developers.hubspot.com/docs/api/client-libraries), using an authorization code that was supplied by HubSpot when you granted access to the app.
Going further with the code
/login Endpoint
/token Endpoint
We hope you found it useful!