Bearer token refresh after HTTP 401 and/or 30 minutes #2023
-
What are strategies for implementing refresh of a bearer token? I'm looking for ways that are both quick and pragmatic. If I can do it at the session level, that would be great. I'm working with an HTTP API that uses basic authentication to request a bearer token. The bearer token remains valid for 30 minutes or invalidated on demand before it expires. The token can be refreshed using a currently-valid token. Once it expires, the API endpoint returns HTTP 401. I've looked into event hooks and custom authentication. Event hooks didn't look like they would help. Custom authentication appears a bit above my current level of understanding, at least from the docs. I could possibly insert some comparison of wall clock time against the token expiration time, but that seems both cumbersome and would affect a lot of code. Background I've got a script using I was also trying to learn The script sets up a (synchronous, not async) session after obtaining the bearer token. I'm unsure how to add in support for refreshing the bearer token when it has expired (HTTP 401) or is possibly near expiration (almost 30 minutes). I want to make the script more robust and handle the possibility of token expiration. I'd also like to make changes in the fewest places possible, so I'm hoping doing something at the session level can apply across all of the functions that make and process API calls. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
A custom authentication class is probably what you want here. I would do something like the example here - store the bearer token as state on the auth class. Any time you get a 401, attempt to refresh the bearer token. Is that enough info to get you started on this? It'd be fab if you could share how you progress on it - no doubt it'll end up being useful to someone else further down the line. |
Beta Was this translation helpful? Give feedback.
-
My head is still spinning trying to understand how to fit this together and adapt it with the code I have. I have a function that creates a session, If I understand the examples correctly, I would need to use the Since I'd be overriding Auth with the class, would I use the class to get the bearer token in all situations — including the initial connection? Instead of passing the username and password two-tuple into Hopefully I'm even bringing up sensible questions. For reference, here's the authentication information. Thanks! |
Beta Was this translation helpful? Give feedback.
-
not sure how far along you are with the implementation, but I'd personally suggest authlib + httpx for oauth applications. see the package tda-api for an implementation. authlib handles the |
Beta Was this translation helpful? Give feedback.
-
@pssolanki111 Thanks, I'll take a look! The API isn't officially Oauth. I've read about Oauth and the flow does seem similar. I don't know if it's close enough for Oauth to work. |
Beta Was this translation helpful? Give feedback.
-
I also have this problem. I solved it by writing a time-based cache of tokens implemented within the slightly weird generator-based async framework of But the API for that is not really up to the job; there are two problems:
At least I can reimplement the |
Beta Was this translation helpful? Give feedback.
A custom authentication class is probably what you want here.
I would do something like the example here - store the bearer token as state on the auth class. Any time you get a 401, attempt to refresh the bearer token.
Is that enough info to get you started on this? It'd be fab if you could share how you progress on it - no doubt it'll end up being useful to someone else further down the line.