Node.js OAuth 2.0 Quickstart
A quickstart app for integrators looking to use HubSpot's OAuth 2.0. Written in Node.js.
Note: This app does not store any data in a persistent way, so restarting the app will clear the retrieved access tokens.
Before running the quickstart app, make sure you have:
- The tools required to run using the method of your choice:
- A free HubSpot developer account (sign up)
- An app associated with your developer account (create an app)
- A HubSpot portal to install the app in (you can use an existing one, or create a test portal)
Note: You must be a super-admin for the portal that you want to install the app in.
What the app does
Redirect to HubSpot's OAuth 2.0 server
When you open your browser to
http://localhost:3000/install, the app will redirect you to the authorization page on HubSpot's server. Here you will choose which portal you'd like to install the app in and give consent for it to act on your behalf. When this is complete, HubSpot will redirect you back to the app.
Exchange an authorization code for access tokens
Now that you're back in the app, it will retrieve an access token and refresh token from HubSpot's server, using an authorization code that was supplied by HubSpot when you granted access to the app.
Retrieve a contact
When the app has received an access token, it will redirect you to
http://localhost:3000/. It will then use the access token to make a query to HubSpot's Contacts API, and display the retrieved contact's name on the page.
Running the app
Option 1: Running in a Docker container
- Build an image of the quickstart app
$ docker build -t hs-oauth-quickstart:latest git://github.com/HubSpot/oauth-quickstart-nodejs.git
- Run a container with the new image
$ docker run --init -it -p 3000:3000 -e CLIENT_SECRET=$CLIENT_SECRET -e CLIENT_ID=$CLIENT_ID -e SCOPE=contacts,forms hs-oauth-quickstart:latest
Option 2: Running natively
- Clone the repository:
$ git clone email@example.com:HubSpot/oauth-quickstart-nodejs.git
- Create a
.envfile in the root of the repository with the ID and secret for your app (found on the app settings page), eg:
You can also add a
CLIENT_ID='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' CLIENT_SECRET='yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy' SCOPE='contacts,forms'
SCOPEenvironment variable to specify a custom set of scopes. The scopes can be separated by a comma, space, or URL-encoded space (
- From the root of the repository, run:
$ yarn install $ yarn start
- Open your browser to
http://localhost:3000/installto kick off the OAuth 2.0 flow