@@ -65,7 +65,7 @@ def __init__(
6565 """
6666 self ._client_id = client_id
6767 self ._client_secret = client_secret
68- self ._store_tokens = store_tokens
68+ self ._store_tokens_callback = store_tokens
6969 self ._access_token = access_token
7070 self ._refresh_token = refresh_token
7171 self ._network_layer = network_layer if network_layer else DefaultNetwork ()
@@ -158,6 +158,17 @@ def _refresh(self, access_token):
158158
159159 return self .send_token_request (data , access_token )
160160
161+ def _get_tokens (self ):
162+ """
163+ Get the current access and refresh tokens.
164+
165+ :return:
166+ Tuple containing the current access token and refresh token.
167+ :rtype:
168+ `tuple` of (`unicode`, `unicode`)
169+ """
170+ return self ._access_token , self ._refresh_token
171+
161172 def refresh (self , access_token_to_refresh ):
162173 """
163174 Refresh the access token and the refresh token and return the access_token, refresh_token tuple. The access
@@ -169,16 +180,17 @@ def refresh(self, access_token_to_refresh):
169180 `unicode`
170181 """
171182 with self ._refresh_lock :
183+ access_token , refresh_token = self ._get_tokens ()
172184 # The lock here is for handling that case that multiple requests fail, due to access token expired, at the
173185 # same time to avoid multiple session renewals.
174- if access_token_to_refresh == self . _access_token :
186+ if access_token_to_refresh == access_token :
175187 # If the active access token is the same as the token needs to be refreshed, we make the request to
176188 # refresh the token.
177189 return self ._refresh (access_token_to_refresh )
178190 else :
179191 # If the active access token (self._access_token) is not the same as the token needs to be refreshed,
180192 # it means the expired token has already been refreshed. Simply return the current active tokens.
181- return self . _access_token , self . _refresh_token
193+ return access_token , refresh_token
182194
183195 @staticmethod
184196 def _get_state_csrf_token ():
@@ -195,6 +207,10 @@ def _get_state_csrf_token():
195207 ascii_len = len (ascii_alphabet )
196208 return 'box_csrf_token_' + '' .join (ascii_alphabet [int (system_random .random () * ascii_len )] for _ in range (16 ))
197209
210+ def _store_tokens (self , access_token , refresh_token ):
211+ if self ._store_tokens_callback is not None :
212+ self ._store_tokens_callback (access_token , refresh_token )
213+
198214 def send_token_request (self , data , access_token , expect_refresh_token = True ):
199215 """
200216 Send the request to acquire or refresh an access token.
@@ -231,6 +247,5 @@ def send_token_request(self, data, access_token, expect_refresh_token=True):
231247 raise BoxOAuthException (network_response .status_code , network_response .content , url , 'POST' )
232248 except (ValueError , KeyError ):
233249 raise BoxOAuthException (network_response .status_code , network_response .content , url , 'POST' )
234- if self ._store_tokens :
235- self ._store_tokens (self ._access_token , self ._refresh_token )
250+ self ._store_tokens (self ._access_token , self ._refresh_token )
236251 return self ._access_token , self ._refresh_token
0 commit comments