APIs & Integrations

Joel36
Member

Retry getting access token gives an error

SOLVE

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!

0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Retry getting access token gives an error

SOLVE

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

View solution in original post

0 Upvotes
3 Replies 3
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Retry getting access token gives an error

SOLVE

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
0 Upvotes
Yogeetha
Member

Retry getting access token gives an error

SOLVE

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

0 Upvotes
lscanlan
HubSpot Alumni
HubSpot Alumni

Retry getting access token gives an error

SOLVE

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
0 Upvotes