I had to do this with a different API the toehr year, and made this method:
def auth_token(self): ''' Obtain a current auth token [...] Refreshes the cached token if stale. ''' while True: with self._auth_token_lock: token = self._auth_token if (token and token.timestamp + FM_TOKEN_LIFESPAN - FM_TOKEN_LIFESPAN_SKEW > time.time()): break try: token = self._get_new_auth_token() except Exception as e: error("error fetching token: %s", e) else: if token: self._auth_token = token break error("token not refreshed, delay then retry") time.sleep(self.DEFAULT_RETRY_DELAY) return token The "while True" bit was because the server was flakey and sometime you just had to wait for it to come back. Then I just called this to obtain a current token whenever I needed a token. in the API, for example: headers = {'Authorization': 'Bearer ' + self.auth_token().token} Means you don't have to embed verbose checks all through your code - just grab "the token" and proceeed. Cheers, Cameron Simpson <c...@cskk.id.au> -- https://mail.python.org/mailman/listinfo/python-list