Retry getting access token gives an error

SOLVE
Highlighted
New Contributor

Hi everyone,

I'm building the integration with HubSpot with OAuth authentication, which is going successfull. I'm able to get the access token and use in the different REST calls. 

Only when I retry getting an access token after an successfull call I get an error: {"status":"BAD_AUTH_CODE","message":"missing or unknown auth code"} . Is this normal behavior? Can you only get an access token once an need to refresh it every time? What is an access token is expired?

Thanks in advance!

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
HubSpot Moderator

Hi @Joel36,

 

If I'm understanding correctly, it sounds like you're generating your access and refresh tokens by POSTing to /oauth/v1/token . You're sending along your authorization code in the request and it works successfully. Except that you then try use the same authorization code again later. Is that correct?

 

You won't be able to use the same authorization code again. They're intended to be short-lived and single use. That's why you're getting an error message indicating a bad auth code: it's already been consumed and exchanged for an access token and a refresh token.

 

After you use the authorization code, you'll want to use the access token returned in that response. You'll also see in the response the amount of seconds until the token expires. It should be 6 hours. After the access token expires, you'll want to use the refresh token in order to generate a new access token. That process is documented here: https://developers.hubspot.com/docs/methods/oauth2/refresh-access-token.

 

Does this help? Let me know if you have any questions about this.

 

- Leland

Leland Scanlan

HubSpot Developer Support
Reply
0 Upvotes
3 Replies 3
HubSpot Moderator

Hi @Joel36,

 

If I'm understanding correctly, it sounds like you're generating your access and refresh tokens by POSTing to /oauth/v1/token . You're sending along your authorization code in the request and it works successfully. Except that you then try use the same authorization code again later. Is that correct?

 

You won't be able to use the same authorization code again. They're intended to be short-lived and single use. That's why you're getting an error message indicating a bad auth code: it's already been consumed and exchanged for an access token and a refresh token.

 

After you use the authorization code, you'll want to use the access token returned in that response. You'll also see in the response the amount of seconds until the token expires. It should be 6 hours. After the access token expires, you'll want to use the refresh token in order to generate a new access token. That process is documented here: https://developers.hubspot.com/docs/methods/oauth2/refresh-access-token.

 

Does this help? Let me know if you have any questions about this.

 

- Leland

Leland Scanlan

HubSpot Developer Support
Reply
0 Upvotes
New Member

Hi! Is there a way to get authorized to HubSpot which has longer access token expiration time. 6 hours is too less that is not feasible solution. Please let know if any other possible ways to authorize hubspot

Reply
0 Upvotes
HubSpot Moderator

Hi @Yogeetha,

 

The expiration time on access tokens is always going to be 6 hours. At least that's how things are set up currently. Could you explain a bit more about why that's an issue for you? Access tokens are intentionally short-lived in oauth, but I'd be curious to learn a bit more about your specific case.

 

Thanks,

Leland

Leland Scanlan

HubSpot Developer Support
Reply
0 Upvotes