def test_refresh_v2_token(self): args = { 'client_id': '111', 'client_secret': '222', 'refresh_token': '333', 'grant_type': 'refresh_token', } (flexmock(requests).should_receive('post').with_args( 'https://www.box.com/api/oauth2/token', args).and_return(flexmock(json=lambda: {'aaa': 'bbb'})).once()) result = refresh_v2_token('111', '222', '333') self.assertDictEqual({'aaa': 'bbb'}, result)
def refresh_oauth_key(external_account, force=False): """If necessary, refreshes the oauth key associated with the external account. """ if external_account.expires_at is None and not force: return if force or (external_account.expires_at - datetime.utcnow()).total_seconds() < settings.REFRESH_TIME: key = refresh_v2_token(settings.BOX_KEY, settings.BOX_SECRET, external_account.refresh_token) external_account.oauth_key = key['access_token'] external_account.refresh_token = key['refresh_token'] external_account.expires_at = datetime.utcfromtimestamp(time.time() + key['expires_in']) external_account.save()
def refresh_access_token(self, force=False): # Ensure that most recent tokens are loaded from the database. Needed # in case another concurrent request has already changed the tokens. if self._is_loaded: try: self.reload() except: pass if self._needs_refresh() or force: token = refresh_v2_token(settings.BOX_KEY, settings.BOX_SECRET, self.refresh_token) self.access_token = token['access_token'] self.refresh_token = token.get('refresh_token', self.refresh_token) self.expires_at = datetime.utcfromtimestamp(time.time() + token['expires_in']) self.save()
def test_refresh_v2_token(self): args = { 'client_id': '111', 'client_secret': '222', 'refresh_token': '333', 'grant_type': 'refresh_token', } (flexmock(requests) .should_receive('post') .with_args('https://www.box.com/api/oauth2/token', args) .and_return(flexmock(json=lambda: {'aaa': 'bbb'})) .once()) result = refresh_v2_token('111', '222', '333') self.assertDictEqual({'aaa': 'bbb'}, result)
def refresh_oauth_key(external_account, force=False): """If necessary, refreshes the oauth key associated with the external account. """ if external_account.expires_at is None and not force: return if force or (external_account.expires_at - datetime.utcnow()).total_seconds() < settings.REFRESH_TIME: key = refresh_v2_token(settings.BOX_KEY, settings.BOX_SECRET, external_account.refresh_token) external_account.oauth_key = key['access_token'] external_account.refresh_token = key['refresh_token'] external_account.expires_at = datetime.utcfromtimestamp( time.time() + key['expires_in']) external_account.save()