def on_refresh_complete(cls, response, id, callback): """Callback for request to get a new access token based on refresh token.""" if response.code in (400, 401): if 'invalid_grant' in response.body: # Our refresh token is invalid, which means that we don't have # permission to access this user's content anymore. Forget them. Cache.delete(cls.auth_cache_key_template % id) Cache.delete(cls.profile_cache_key_template % id) TokenIdMapping.remove_id(id) logging.error("Access was revoked for %s; cached data deleted.", id) logging.error("HTTP %s while trying to refresh access token for %s.", response.code, id) return IOLoop.instance().add_callback(lambda: callback(None)) elif response.code != 200: logging.error("Non-200 response to refresh token request (%s, id=%s): %r" % (response.code, id, response.body)) return IOLoop.instance().add_callback(lambda: callback(None)) results = json.loads(response.body) # sanity check if results['token_type'] != "Bearer": logging.error('Unknown token type received: %s' % results['token_type']) return IOLoop.instance().add_callback(lambda: callback(None)) token = results['access_token'] Cache.set(cls.auth_cache_key_template % id, token, time=results['expires_in']) IOLoop.instance().add_callback(lambda: callback(token))
def wrap_callback(response): if response.code == 401: Cache.delete(cls.auth_cache_key_template % user_id) # Retry once to see if we can use a refresh token to get a new key. if _authed_fetch_retry: cls.authed_fetch(user_id, url, callback, _authed_fetch_retry=False, *args, **kwargs) else: logging.warning("Abandoning request for %s after retry.", url) else: return callback(response)