Reputation:
I'm using Stripe API and I'm trying to save the connect account keys in db. But I cannot save them successfully and a weird thing is happening. My code is here
resp = stripe_connect_service.get_raw_access_token(method='POST', data=data)
connect_account_info = json.loads(resp.text)
connect_public_key = connect_account_info['stripe_publishable_key']
connect_access_token = connect_account_info['access_token']
connect_user_id = connect_account_info['stripe_user_id']
connect_refresh_token = connect_account_info['refresh_token']
print(connect_public_key)
print(connect_access_token)
print(connect_user_id)
print(connect_refresh_token)
form = Form()
if form.validate_on_submit():
data = Data(connect_public_key=connect_public_key, connect_access_token=connect_access_token, connect_user_id=connect_user_id, connect_refresh_token=connect_refresh_token)
db.session.add(data)
db.session.commit()
So after getting the token and finished submitting the form on the page rediercted from Stripe Connect page, I will save the data. print actually shows the connect account keys, but after submitting the form, the error says connect_public_key = connect_account_info['stripe_publishable_key'] KeyError: 'stripe_publishable_key' even though print does work.
And when I run debugger, the error says like this
{'error': 'invalid_grant', 'error_description': 'This authorization code has already been used.
How can I fix this error?
Upvotes: 2
Views: 2620
Reputation: 10715
In my case it was my server that retried on failure that eventually leads to this error response.
So in my case the problem was that my server's function crashed in a different place, after getting the token from the code.
And after the function crashed, since the server was setup for retry, it called the endpoint once again with the same params and in the second attempt the code was already used indeed, hence the error.
Maybe it can help someone too.
Upvotes: 0
Reputation: 25552
This error usually happens when your code incorrectly re-uses the authorization code (ac_XXXX) that you get in the URL.
This is often caused by refreshing the browser/page you're on after the redirect. Your code will fetch the code from the URL and use it to exchange it on the /oauth/token
endpoint but if you do it twice it causes the connection to be revoked for security reasons.
Upvotes: 1