def testrefresh(self): oauth.refresh('REFRESH', dwollaparse='dict') oauth.r._post_without_token.assert_any_call('/token/', { 'client_secret': 'SOME SECRET', 'grant_type': 'refresh_token', 'refresh_token': 'REFRESH', 'client_id': 'SOME ID' }, {'dwollaparse': 'dict'}, custompostfix='/oauth/v2')
def dwolla_update_tokens(days): """ Refreshes all tokens expiring within the next <days> days. """ start = timezone.now() end = start + datetime.timedelta(days=days) count = 0 test_count = 0 from brambling.models import Organization, Person, Order for api_type in (LIVE, TEST): dwolla_prep(api_type) if api_type == LIVE: field = "dwolla_refresh_token" access_expires = "dwolla_access_token_expires" else: field = "dwolla_test_refresh_token" access_expires = "dwolla_test_access_token_expires" kwargs = {field + "_expires__range": (start, end), access_expires + "__lt": start} for model in (Organization, Person, Order): qs = model.objects.filter(**kwargs) for item in qs: refresh_token = getattr(item, field) oauth_data = oauth.refresh(refresh_token) dwolla_set_tokens(item, api_type, oauth_data) item.save() if api_type == LIVE: count += 1 else: test_count += 1 return count, test_count
def dwolla_get_token(dwolla_obj, api_type): """ Gets a working dwolla access token for the correct api, refreshing if necessary. """ if api_type == LIVE: expires = dwolla_obj.dwolla_access_token_expires refresh_expires = dwolla_obj.dwolla_refresh_token_expires else: expires = dwolla_obj.dwolla_test_access_token_expires refresh_expires = dwolla_obj.dwolla_test_refresh_token_expires if expires is None or refresh_expires is None: raise ValueError("Invalid dwolla object - unknown token expiration.") now = timezone.now() if expires < now: if refresh_expires < now: dwolla_obj.clear_dwolla_data(api_type) dwolla_obj.save() raise ValueError("Token is expired and can't be refreshed.") if api_type == LIVE: refresh_token = dwolla_obj.dwolla_refresh_token else: refresh_token = dwolla_obj.dwolla_test_refresh_token oauth_data = oauth.refresh(refresh_token) dwolla_set_tokens(dwolla_obj, api_type, oauth_data) dwolla_obj.save() if api_type == LIVE: access_token = dwolla_obj.dwolla_access_token else: access_token = dwolla_obj.dwolla_test_access_token return access_token
def dwolla_update_tokens(days): """ Refreshes or clears all tokens that will not be refreshable within the next <days> days. """ end = timezone.now() + datetime.timedelta(days=days) count = 0 invalid_count = 0 test_count = 0 invalid_test_count = 0 from brambling.models import DwollaAccount accounts = DwollaAccount.objects.filter( refresh_token_expires__lt=end, is_valid=True, ) for account in accounts: refresh_token = account.refresh_token dwolla_prep(account.api_type) oauth_data = oauth.refresh(refresh_token) try: account.set_tokens(oauth_data) except ValueError: account.is_valid = False if account.api_type == LIVE: invalid_count += 1 else: invalid_test_count += 1 else: if account.api_type == LIVE: count += 1 else: test_count += 1 account.save() return count, invalid_count, test_count, invalid_test_count
# http://requestb.in is a service that catches # redirect responses. Go over to their URL and make # your own so that you may conveniently catch the # redirect parameters. # # You can view your responses at: # http://requestb.in/[some_id]?inspect # # If you're feeling dangerous, feel free to simply use # http://google.com and manually parse the parameters # out yourself. The choice remains yours. print oauth.genauthurl("http://requestb.in/yxlywryx") # Step 2: The redirect should provide you with a `code` # parameter. You will now exchange this code for an access # and refresh token pair. access_set = oauth.get("Z/KHDIyWO/LboIGn3wGGs1+sRWg=", "http://requestb.in/yxlywryx") print access_set # Step 2.5: If you wish, you can set the library's global # access token parameter by doing the following... constants.access_token=access_set['access_token'] # Step 3: Exchange your expiring refresh token for another # access/refresh token pair.x print oauth.refresh(access_set['refresh_token'])
def testrefresh(self): oauth.refresh('REFRESH', dwollaparse='dict') oauth.r._post_without_token.assert_any_call('/token/', {'client_secret': 'SOME SECRET', 'grant_type': 'refresh_token', 'refresh_token': 'REFRESH', 'client_id': 'SOME ID'}, {'dwollaparse': 'dict'}, custompostfix='/oauth/v2')
# # If you're feeling dangerous, feel free to simply use # http://google.com and manually parse the parameters # out yourself. The choice remains yours. print(oauth.genauthurl("http://requestb.in/122rdhc1")) # Step 2: The redirect should provide you with a `code` # parameter. You will now exchange this code for an access # and refresh token pair. access_set = oauth.get("Z/KHDIyWO/LboIGn3wGGs1+sRWg=", "http://requestb.in/122rdhc1") print(access_set) # Step 2.5: If you wish, you can set the library's global # access token parameter by doing the following... constants.access_token = access_set['access_token'] # Step 3: Exchange your expiring refresh token for another # access/refresh token pair.x access_set = oauth.refresh(access_set['refresh_token']) print(access_set) # Step 4: Retrieve the catalog of endpoints that # are available to the OAuth token which you just # retrieved. print(oauth.catalog())
def testrefresh(self): oauth.refresh('REFRESH') oauth.r._post.assert_any_call('/token/', {'client_secret': 'SOME SECRET', 'grant_type': 'refresh_token', 'refresh_token': 'REFRESH', 'client_id': 'SOME ID'}, '/oauth/v2', 'dict')