What "state" query param am I missing?

I have picked up support for some Hubspot applications and am attempting to generate a Refresh Token to use with some custom Python integrations we are using.
But when I am going through the OAuth process, I am hitting this error…
"

Failure

An error ocurred while installing the Hubspot app.

Missing required state query param"
It *briefly* loads up the authorization screen, and then quickly crashes to this. I’m not seeing a whole lot of stuff to work from here.
I have the app id and everything, but not sure what’s going wrong.

Hello @RTaylor60

The “state” query parameter is a security feature used in the OAuth 2.0 flow to prevent CSRF (Cross-Site Request Forgery) attacks. When a user initiates an OAuth 2.0 flow, a state parameter is included in the initial authorization request, and this value is then returned to your application when the user is redirected back after completing the authorization process. Your application should then verify that the state parameter returned by HubSpot matches the one you originally sent to ensure that the request was not tampered with.

Based on the error message you are seeing, it appears that the state parameter is missing from your initial authorization request. To fix this, you should ensure that your authorization request includes a state parameter with a unique value that you generate. You can then verify this value when the user is redirected back to your application after completing the authorization process.

In Python, you can generate a random string for the state parameter using the secrets module. Here’s an example of how you can generate a random 32-character string for the state parameter:

import secrets

state = secrets.token_urlsafe(32)

You should then include this state parameter in your authorization request URL as a query parameter, like this:

https://app.hubspot.com/oauth/authorize?client_id=<your_client_id>&scope=<your_scopes>&redirect_uri=<your_redirect_uri>&state=<your_state>

Make sure that the state parameter is unique for each authorization request you make. This should prevent the “Missing required state query param” error you are seeing and allow you to complete the OAuth 2.0 flow successfully.

hi @himanshurauthan
we noticed today that if user logs in to hubspot, state is not returned back to the application, but if the user is already authenticated state is returned to the application.

Is that something that recently changed on your side ? could you help with that please. thanks

Thanks for the detailed response. So I do currently have a state that is begin generated and passed from the callback URI…but this..
>a state parameter is included in the initial authorization request
Is this is the one that is generated by the URL provided in the Application Auth screen in Hubspot?
So it seems to be breaking on the redirect. We were able to grab the authorization code that is being sent, and we were able to use that to get a new Refresh Token for our new account for the application (the end goal of all this), but the flow is still broken for our app.
My apologies if I’m not describing all this correctly. I picked up support for this application and hadn’t previously used Hubspot at all.

hi @himanshurauthan

I also encountered the same problem, could you help with that please. thanks