<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Outh: Sometimes Code verification failes in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Outh-Sometimes-Code-verification-failes/m-p/795564#M64106</link>
    <description>&lt;P&gt;Hello &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/525929"&gt;@CalliperTony&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I can understand your problem. I have been giving you a problem-solution code. It can help you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;require('dotenv').config();&lt;BR /&gt;const express = require('express');&lt;BR /&gt;const request = require('request-promise-native');&lt;BR /&gt;const NodeCache = require('node-cache');&lt;BR /&gt;const session = require('express-session');&lt;BR /&gt;const opn = require('open');&lt;BR /&gt;const app = express();&lt;BR /&gt;&lt;BR /&gt;const PORT = 3000;&lt;BR /&gt;&lt;BR /&gt;const refreshTokenStore = {};&lt;BR /&gt;const accessTokenCache = new NodeCache({ deleteOnExpire: true });&lt;BR /&gt;&lt;BR /&gt;if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET) {&lt;BR /&gt;throw new Error('Missing CLIENT_ID or CLIENT_SECRET environment variable.')&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;//===========================================================================//&lt;BR /&gt;// HUBSPOT APP CONFIGURATION&lt;BR /&gt;//&lt;BR /&gt;// All the following values must match configuration settings in your app.&lt;BR /&gt;// They will be used to build the OAuth URL, which users visit to begin&lt;BR /&gt;// installing. If they don't match your app's configuration, users will&lt;BR /&gt;// see an error page.&lt;BR /&gt;&lt;BR /&gt;// Replace the following with the values from your app auth config, &lt;BR /&gt;// or set them as environment variables before running.&lt;BR /&gt;const CLIENT_ID = process.env.CLIENT_ID;&lt;BR /&gt;const CLIENT_SECRET = process.env.CLIENT_SECRET;&lt;BR /&gt;&lt;BR /&gt;// Scopes for this app will default to `crm.objects.contacts.read`&lt;BR /&gt;// To request others, set the SCOPE environment variable instead&lt;BR /&gt;let SCOPES = ['crm.objects.contacts.read'];&lt;BR /&gt;if (process.env.SCOPE) {&lt;BR /&gt;SCOPES = (process.env.SCOPE.split(/ |, ?|%20/)).join(' ');&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// On successful install, users will be redirected to /oauth-callback&lt;BR /&gt;const REDIRECT_URI = `http://localhost:${PORT}/oauth-callback`;&lt;BR /&gt;&lt;BR /&gt;//===========================================================================//&lt;BR /&gt;&lt;BR /&gt;// Use a session to keep track of client ID&lt;BR /&gt;app.use(session({&lt;BR /&gt;secret: Math.random().toString(36).substring(2),&lt;BR /&gt;resave: false,&lt;BR /&gt;saveUninitialized: true&lt;BR /&gt;}));&lt;BR /&gt;&lt;BR /&gt;//================================//&lt;BR /&gt;// Running the OAuth 2.0 Flow //&lt;BR /&gt;//================================//&lt;BR /&gt;&lt;BR /&gt;// Step 1&lt;BR /&gt;// Build the authorization URL to redirect a user&lt;BR /&gt;// to when they choose to install the app&lt;BR /&gt;const authUrl =&lt;BR /&gt;'https://app.hubspot.com/oauth/authorize' +&lt;BR /&gt;`?client_id=${encodeURIComponent(CLIENT_ID)}` + // app's client ID&lt;BR /&gt;`&amp;amp;scope=${encodeURIComponent(SCOPES)}` + // scopes being requested by the app&lt;BR /&gt;`&amp;amp;redirect_uri=${encodeURIComponent(REDIRECT_URI)}`; // where to send the user after the consent page&lt;BR /&gt;&lt;BR /&gt;// Redirect the user from the installation page to&lt;BR /&gt;// the authorization URL&lt;BR /&gt;app.get('/install', (req, res) =&amp;gt; {&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log('=== Initiating OAuth 2.0 flow with HubSpot ===');&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log("===&amp;gt; Step 1: Redirecting user to your app's OAuth URL");&lt;BR /&gt;res.redirect(authUrl);&lt;BR /&gt;console.log('===&amp;gt; Step 2: User is being prompted for consent by HubSpot');&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;// Step 2&lt;BR /&gt;// The user is prompted to give the app access to the requested&lt;BR /&gt;// resources. This is all done by HubSpot, so no work is necessary&lt;BR /&gt;// on the app's end&lt;BR /&gt;&lt;BR /&gt;// Step 3&lt;BR /&gt;// Receive the authorization code from the OAuth 2.0 Server,&lt;BR /&gt;// and process it based on the query parameters that are passed&lt;BR /&gt;app.get('/oauth-callback', async (req, res) =&amp;gt; {&lt;BR /&gt;console.log('===&amp;gt; Step 3: Handling the request sent by the server');&lt;BR /&gt;&lt;BR /&gt;// Received a user authorization code, so now combine that with the other&lt;BR /&gt;// required values and exchange both for an access token and a refresh token&lt;BR /&gt;if (req.query.code) {&lt;BR /&gt;console.log(' &amp;gt; Received an authorization token');&lt;BR /&gt;&lt;BR /&gt;const authCodeProof = {&lt;BR /&gt;grant_type: 'authorization_code',&lt;BR /&gt;client_id: CLIENT_ID,&lt;BR /&gt;client_secret: CLIENT_SECRET,&lt;BR /&gt;redirect_uri: REDIRECT_URI,&lt;BR /&gt;code: req.query.code&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;// Step 4&lt;BR /&gt;// Exchange the authorization code for an access token and refresh token&lt;BR /&gt;console.log('===&amp;gt; Step 4: Exchanging authorization code for an access token and refresh token');&lt;BR /&gt;const token = await exchangeForTokens(req.sessionID, authCodeProof);&lt;BR /&gt;if (token.message) {&lt;BR /&gt;return res.redirect(`/error?msg=${token.message}`);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// Once the tokens have been retrieved, use them to make a query&lt;BR /&gt;// to the HubSpot API&lt;BR /&gt;res.redirect(`/`);&lt;BR /&gt;}&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;//==========================================//&lt;BR /&gt;// Exchanging Proof for an Access Token //&lt;BR /&gt;//==========================================//&lt;BR /&gt;&lt;BR /&gt;const exchangeForTokens = async (userId, exchangeProof) =&amp;gt; {&lt;BR /&gt;try {&lt;BR /&gt;const responseBody = await request.post('https://api.hubapi.com/oauth/v1/token', {&lt;BR /&gt;form: exchangeProof&lt;BR /&gt;});&lt;BR /&gt;// Usually, this token data should be persisted in a database and associated with&lt;BR /&gt;// a user identity.&lt;BR /&gt;const tokens = JSON.parse(responseBody);&lt;BR /&gt;refreshTokenStore[userId] = tokens.refresh_token;&lt;BR /&gt;accessTokenCache.set(userId, tokens.access_token, Math.round(tokens.expires_in * 0.75));&lt;BR /&gt;&lt;BR /&gt;console.log(' &amp;gt; Received an access token and refresh token');&lt;BR /&gt;return tokens.access_token;&lt;BR /&gt;} catch (e) {&lt;BR /&gt;console.error(` &amp;gt; Error exchanging ${exchangeProof.grant_type} for access token`);&lt;BR /&gt;return JSON.parse(e.response.body);&lt;BR /&gt;}&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const refreshAccessToken = async (userId) =&amp;gt; {&lt;BR /&gt;const refreshTokenProof = {&lt;BR /&gt;grant_type: 'refresh_token',&lt;BR /&gt;client_id: CLIENT_ID,&lt;BR /&gt;client_secret: CLIENT_SECRET,&lt;BR /&gt;redirect_uri: REDIRECT_URI,&lt;BR /&gt;refresh_token: refreshTokenStore[userId]&lt;BR /&gt;};&lt;BR /&gt;return await exchangeForTokens(userId, refreshTokenProof);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const getAccessToken = async (userId) =&amp;gt; {&lt;BR /&gt;// If the access token has expired, retrieve&lt;BR /&gt;// a new one using the refresh token&lt;BR /&gt;if (!accessTokenCache.get(userId)) {&lt;BR /&gt;console.log('Refreshing expired access token');&lt;BR /&gt;await refreshAccessToken(userId);&lt;BR /&gt;}&lt;BR /&gt;return accessTokenCache.get(userId);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const isAuthorized = (userId) =&amp;gt; {&lt;BR /&gt;return refreshTokenStore[userId] ? true : false;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;//====================================================//&lt;BR /&gt;// Using an Access Token to Query the HubSpot API //&lt;BR /&gt;//====================================================//&lt;BR /&gt;&lt;BR /&gt;const getContact = async (accessToken) =&amp;gt; {&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log('=== Retrieving a contact from HubSpot using the access token ===');&lt;BR /&gt;try {&lt;BR /&gt;const headers = {&lt;BR /&gt;Authorization: `Bearer ${accessToken}`,&lt;BR /&gt;'Content-Type': 'application/json'&lt;BR /&gt;};&lt;BR /&gt;console.log('===&amp;gt; Replace the following request.get() to test other API calls');&lt;BR /&gt;console.log('===&amp;gt; request.get(\'https://api.hubapi.com/contacts/v1/lists/all/contacts/all?count=1\')');&lt;BR /&gt;const result = await request.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?count=1', {&lt;BR /&gt;headers: headers&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;return JSON.parse(result).contacts[0];&lt;BR /&gt;} catch (e) {&lt;BR /&gt;console.error(' &amp;gt; Unable to retrieve contact');&lt;BR /&gt;return JSON.parse(e.response.body);&lt;BR /&gt;}&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;//========================================//&lt;BR /&gt;// Displaying information to the user //&lt;BR /&gt;//========================================//&lt;BR /&gt;&lt;BR /&gt;const displayContactName = (res, contact) =&amp;gt; {&lt;BR /&gt;if (contact.status === 'error') {&lt;BR /&gt;res.write(`&amp;lt;p&amp;gt;Unable to retrieve contact! Error Message: ${contact.message}&amp;lt;/p&amp;gt;`);&lt;BR /&gt;return;&lt;BR /&gt;}&lt;BR /&gt;const { firstname, lastname } = contact.properties;&lt;BR /&gt;res.write(`&amp;lt;p&amp;gt;Contact name: ${firstname.value} ${lastname.value}&amp;lt;/p&amp;gt;`);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;app.get('/', async (req, res) =&amp;gt; {&lt;BR /&gt;res.setHeader('Content-Type', 'text/html');&lt;BR /&gt;res.write(`&amp;lt;h2&amp;gt;HubSpot OAuth 2.0 Quickstart App&amp;lt;/h2&amp;gt;`);&lt;BR /&gt;if (isAuthorized(req.sessionID)) {&lt;BR /&gt;const accessToken = await getAccessToken(req.sessionID);&lt;BR /&gt;const contact = await getContact(accessToken);&lt;BR /&gt;res.write(`&amp;lt;h4&amp;gt;Access token: ${accessToken}&amp;lt;/h4&amp;gt;`);&lt;BR /&gt;displayContactName(res, contact);&lt;BR /&gt;} else {&lt;BR /&gt;res.write(`&amp;lt;a href="/install"&amp;gt;&amp;lt;h3&amp;gt;Install the app&amp;lt;/h3&amp;gt;&amp;lt;/a&amp;gt;`);&lt;BR /&gt;}&lt;BR /&gt;res.end();&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;app.get('/error', (req, res) =&amp;gt; {&lt;BR /&gt;res.setHeader('Content-Type', 'text/html');&lt;BR /&gt;res.write(`&amp;lt;h4&amp;gt;Error: ${req.query.msg}&amp;lt;/h4&amp;gt;`);&lt;BR /&gt;res.end();&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;app.listen(PORT, () =&amp;gt; console.log(`=== Starting your app on http://localhost:${PORT} ===`));&lt;BR /&gt;opn(`http://localhost:${PORT}`);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;References Link : &lt;A href="https://github.com/HubSpot/oauth-quickstart-nodejs/blob/master/index.js" target="_blank" rel="noopener"&gt;https://github.com/HubSpot/oauth-quickstart-nodejs/blob/master/index.js&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope it can help you.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/89212i476997819B6DE825/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 18 May 2023 12:59:19 GMT</pubDate>
    <dc:creator>ankitparmar09</dc:creator>
    <dc:date>2023-05-18T12:59:19Z</dc:date>
    <item>
      <title>Outh: Sometimes Code verification failes</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Outh-Sometimes-Code-verification-failes/m-p/759774#M61612</link>
      <description>&lt;P&gt;In roughly 20% of cases trying to get tokens by code fails with "Incorrect code error"&lt;/P&gt;</description>
      <pubDate>Wed, 22 Feb 2023 20:30:32 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Outh-Sometimes-Code-verification-failes/m-p/759774#M61612</guid>
      <dc:creator>CalliperTony</dc:creator>
      <dc:date>2023-02-22T20:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Outh: Sometimes Code verification failes</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Outh-Sometimes-Code-verification-failes/m-p/795564#M64106</link>
      <description>&lt;P&gt;Hello &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/525929"&gt;@CalliperTony&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, I can understand your problem. I have been giving you a problem-solution code. It can help you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;require('dotenv').config();&lt;BR /&gt;const express = require('express');&lt;BR /&gt;const request = require('request-promise-native');&lt;BR /&gt;const NodeCache = require('node-cache');&lt;BR /&gt;const session = require('express-session');&lt;BR /&gt;const opn = require('open');&lt;BR /&gt;const app = express();&lt;BR /&gt;&lt;BR /&gt;const PORT = 3000;&lt;BR /&gt;&lt;BR /&gt;const refreshTokenStore = {};&lt;BR /&gt;const accessTokenCache = new NodeCache({ deleteOnExpire: true });&lt;BR /&gt;&lt;BR /&gt;if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET) {&lt;BR /&gt;throw new Error('Missing CLIENT_ID or CLIENT_SECRET environment variable.')&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;//===========================================================================//&lt;BR /&gt;// HUBSPOT APP CONFIGURATION&lt;BR /&gt;//&lt;BR /&gt;// All the following values must match configuration settings in your app.&lt;BR /&gt;// They will be used to build the OAuth URL, which users visit to begin&lt;BR /&gt;// installing. If they don't match your app's configuration, users will&lt;BR /&gt;// see an error page.&lt;BR /&gt;&lt;BR /&gt;// Replace the following with the values from your app auth config, &lt;BR /&gt;// or set them as environment variables before running.&lt;BR /&gt;const CLIENT_ID = process.env.CLIENT_ID;&lt;BR /&gt;const CLIENT_SECRET = process.env.CLIENT_SECRET;&lt;BR /&gt;&lt;BR /&gt;// Scopes for this app will default to `crm.objects.contacts.read`&lt;BR /&gt;// To request others, set the SCOPE environment variable instead&lt;BR /&gt;let SCOPES = ['crm.objects.contacts.read'];&lt;BR /&gt;if (process.env.SCOPE) {&lt;BR /&gt;SCOPES = (process.env.SCOPE.split(/ |, ?|%20/)).join(' ');&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// On successful install, users will be redirected to /oauth-callback&lt;BR /&gt;const REDIRECT_URI = `http://localhost:${PORT}/oauth-callback`;&lt;BR /&gt;&lt;BR /&gt;//===========================================================================//&lt;BR /&gt;&lt;BR /&gt;// Use a session to keep track of client ID&lt;BR /&gt;app.use(session({&lt;BR /&gt;secret: Math.random().toString(36).substring(2),&lt;BR /&gt;resave: false,&lt;BR /&gt;saveUninitialized: true&lt;BR /&gt;}));&lt;BR /&gt;&lt;BR /&gt;//================================//&lt;BR /&gt;// Running the OAuth 2.0 Flow //&lt;BR /&gt;//================================//&lt;BR /&gt;&lt;BR /&gt;// Step 1&lt;BR /&gt;// Build the authorization URL to redirect a user&lt;BR /&gt;// to when they choose to install the app&lt;BR /&gt;const authUrl =&lt;BR /&gt;'https://app.hubspot.com/oauth/authorize' +&lt;BR /&gt;`?client_id=${encodeURIComponent(CLIENT_ID)}` + // app's client ID&lt;BR /&gt;`&amp;amp;scope=${encodeURIComponent(SCOPES)}` + // scopes being requested by the app&lt;BR /&gt;`&amp;amp;redirect_uri=${encodeURIComponent(REDIRECT_URI)}`; // where to send the user after the consent page&lt;BR /&gt;&lt;BR /&gt;// Redirect the user from the installation page to&lt;BR /&gt;// the authorization URL&lt;BR /&gt;app.get('/install', (req, res) =&amp;gt; {&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log('=== Initiating OAuth 2.0 flow with HubSpot ===');&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log("===&amp;gt; Step 1: Redirecting user to your app's OAuth URL");&lt;BR /&gt;res.redirect(authUrl);&lt;BR /&gt;console.log('===&amp;gt; Step 2: User is being prompted for consent by HubSpot');&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;// Step 2&lt;BR /&gt;// The user is prompted to give the app access to the requested&lt;BR /&gt;// resources. This is all done by HubSpot, so no work is necessary&lt;BR /&gt;// on the app's end&lt;BR /&gt;&lt;BR /&gt;// Step 3&lt;BR /&gt;// Receive the authorization code from the OAuth 2.0 Server,&lt;BR /&gt;// and process it based on the query parameters that are passed&lt;BR /&gt;app.get('/oauth-callback', async (req, res) =&amp;gt; {&lt;BR /&gt;console.log('===&amp;gt; Step 3: Handling the request sent by the server');&lt;BR /&gt;&lt;BR /&gt;// Received a user authorization code, so now combine that with the other&lt;BR /&gt;// required values and exchange both for an access token and a refresh token&lt;BR /&gt;if (req.query.code) {&lt;BR /&gt;console.log(' &amp;gt; Received an authorization token');&lt;BR /&gt;&lt;BR /&gt;const authCodeProof = {&lt;BR /&gt;grant_type: 'authorization_code',&lt;BR /&gt;client_id: CLIENT_ID,&lt;BR /&gt;client_secret: CLIENT_SECRET,&lt;BR /&gt;redirect_uri: REDIRECT_URI,&lt;BR /&gt;code: req.query.code&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;// Step 4&lt;BR /&gt;// Exchange the authorization code for an access token and refresh token&lt;BR /&gt;console.log('===&amp;gt; Step 4: Exchanging authorization code for an access token and refresh token');&lt;BR /&gt;const token = await exchangeForTokens(req.sessionID, authCodeProof);&lt;BR /&gt;if (token.message) {&lt;BR /&gt;return res.redirect(`/error?msg=${token.message}`);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// Once the tokens have been retrieved, use them to make a query&lt;BR /&gt;// to the HubSpot API&lt;BR /&gt;res.redirect(`/`);&lt;BR /&gt;}&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;//==========================================//&lt;BR /&gt;// Exchanging Proof for an Access Token //&lt;BR /&gt;//==========================================//&lt;BR /&gt;&lt;BR /&gt;const exchangeForTokens = async (userId, exchangeProof) =&amp;gt; {&lt;BR /&gt;try {&lt;BR /&gt;const responseBody = await request.post('https://api.hubapi.com/oauth/v1/token', {&lt;BR /&gt;form: exchangeProof&lt;BR /&gt;});&lt;BR /&gt;// Usually, this token data should be persisted in a database and associated with&lt;BR /&gt;// a user identity.&lt;BR /&gt;const tokens = JSON.parse(responseBody);&lt;BR /&gt;refreshTokenStore[userId] = tokens.refresh_token;&lt;BR /&gt;accessTokenCache.set(userId, tokens.access_token, Math.round(tokens.expires_in * 0.75));&lt;BR /&gt;&lt;BR /&gt;console.log(' &amp;gt; Received an access token and refresh token');&lt;BR /&gt;return tokens.access_token;&lt;BR /&gt;} catch (e) {&lt;BR /&gt;console.error(` &amp;gt; Error exchanging ${exchangeProof.grant_type} for access token`);&lt;BR /&gt;return JSON.parse(e.response.body);&lt;BR /&gt;}&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const refreshAccessToken = async (userId) =&amp;gt; {&lt;BR /&gt;const refreshTokenProof = {&lt;BR /&gt;grant_type: 'refresh_token',&lt;BR /&gt;client_id: CLIENT_ID,&lt;BR /&gt;client_secret: CLIENT_SECRET,&lt;BR /&gt;redirect_uri: REDIRECT_URI,&lt;BR /&gt;refresh_token: refreshTokenStore[userId]&lt;BR /&gt;};&lt;BR /&gt;return await exchangeForTokens(userId, refreshTokenProof);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const getAccessToken = async (userId) =&amp;gt; {&lt;BR /&gt;// If the access token has expired, retrieve&lt;BR /&gt;// a new one using the refresh token&lt;BR /&gt;if (!accessTokenCache.get(userId)) {&lt;BR /&gt;console.log('Refreshing expired access token');&lt;BR /&gt;await refreshAccessToken(userId);&lt;BR /&gt;}&lt;BR /&gt;return accessTokenCache.get(userId);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;const isAuthorized = (userId) =&amp;gt; {&lt;BR /&gt;return refreshTokenStore[userId] ? true : false;&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;//====================================================//&lt;BR /&gt;// Using an Access Token to Query the HubSpot API //&lt;BR /&gt;//====================================================//&lt;BR /&gt;&lt;BR /&gt;const getContact = async (accessToken) =&amp;gt; {&lt;BR /&gt;console.log('');&lt;BR /&gt;console.log('=== Retrieving a contact from HubSpot using the access token ===');&lt;BR /&gt;try {&lt;BR /&gt;const headers = {&lt;BR /&gt;Authorization: `Bearer ${accessToken}`,&lt;BR /&gt;'Content-Type': 'application/json'&lt;BR /&gt;};&lt;BR /&gt;console.log('===&amp;gt; Replace the following request.get() to test other API calls');&lt;BR /&gt;console.log('===&amp;gt; request.get(\'https://api.hubapi.com/contacts/v1/lists/all/contacts/all?count=1\')');&lt;BR /&gt;const result = await request.get('https://api.hubapi.com/contacts/v1/lists/all/contacts/all?count=1', {&lt;BR /&gt;headers: headers&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;return JSON.parse(result).contacts[0];&lt;BR /&gt;} catch (e) {&lt;BR /&gt;console.error(' &amp;gt; Unable to retrieve contact');&lt;BR /&gt;return JSON.parse(e.response.body);&lt;BR /&gt;}&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;//========================================//&lt;BR /&gt;// Displaying information to the user //&lt;BR /&gt;//========================================//&lt;BR /&gt;&lt;BR /&gt;const displayContactName = (res, contact) =&amp;gt; {&lt;BR /&gt;if (contact.status === 'error') {&lt;BR /&gt;res.write(`&amp;lt;p&amp;gt;Unable to retrieve contact! Error Message: ${contact.message}&amp;lt;/p&amp;gt;`);&lt;BR /&gt;return;&lt;BR /&gt;}&lt;BR /&gt;const { firstname, lastname } = contact.properties;&lt;BR /&gt;res.write(`&amp;lt;p&amp;gt;Contact name: ${firstname.value} ${lastname.value}&amp;lt;/p&amp;gt;`);&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;app.get('/', async (req, res) =&amp;gt; {&lt;BR /&gt;res.setHeader('Content-Type', 'text/html');&lt;BR /&gt;res.write(`&amp;lt;h2&amp;gt;HubSpot OAuth 2.0 Quickstart App&amp;lt;/h2&amp;gt;`);&lt;BR /&gt;if (isAuthorized(req.sessionID)) {&lt;BR /&gt;const accessToken = await getAccessToken(req.sessionID);&lt;BR /&gt;const contact = await getContact(accessToken);&lt;BR /&gt;res.write(`&amp;lt;h4&amp;gt;Access token: ${accessToken}&amp;lt;/h4&amp;gt;`);&lt;BR /&gt;displayContactName(res, contact);&lt;BR /&gt;} else {&lt;BR /&gt;res.write(`&amp;lt;a href="/install"&amp;gt;&amp;lt;h3&amp;gt;Install the app&amp;lt;/h3&amp;gt;&amp;lt;/a&amp;gt;`);&lt;BR /&gt;}&lt;BR /&gt;res.end();&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;app.get('/error', (req, res) =&amp;gt; {&lt;BR /&gt;res.setHeader('Content-Type', 'text/html');&lt;BR /&gt;res.write(`&amp;lt;h4&amp;gt;Error: ${req.query.msg}&amp;lt;/h4&amp;gt;`);&lt;BR /&gt;res.end();&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;app.listen(PORT, () =&amp;gt; console.log(`=== Starting your app on http://localhost:${PORT} ===`));&lt;BR /&gt;opn(`http://localhost:${PORT}`);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;References Link : &lt;A href="https://github.com/HubSpot/oauth-quickstart-nodejs/blob/master/index.js" target="_blank" rel="noopener"&gt;https://github.com/HubSpot/oauth-quickstart-nodejs/blob/master/index.js&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope it can help you.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 999px;"&gt;&lt;img src="https://community.hubspot.com/t5/image/serverpage/image-id/89212i476997819B6DE825/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2023 12:59:19 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Outh-Sometimes-Code-verification-failes/m-p/795564#M64106</guid>
      <dc:creator>ankitparmar09</dc:creator>
      <dc:date>2023-05-18T12:59:19Z</dc:date>
    </item>
  </channel>
</rss>

