Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added retry logic to token refresh to deal with #672

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions databricks/sdk/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,23 @@ def refresh(self) -> Token:
raise ValueError('oauth2: token expired and refresh token is not set')
params = {'grant_type': 'refresh_token', 'refresh_token': refresh_token}
headers = {}
if 'microsoft' in self._client.token_url:
# Tokens issued for the 'Single-Page Application' client-type may
# only be redeemed via cross-origin requests
headers = {'Origin': self._client.redirect_url}
return retrieve_token(client_id=self._client.client_id,
client_secret=self._client.client_secret,
token_url=self._client.token_url,
params=params,
use_params=True,
headers=headers)
while True:
try:
return retrieve_token(client_id=self._client.client_id,
client_secret=self._client.client_secret,
token_url=self._client.token_url,
params=params,
use_params=True,
headers=headers)
except ValueError as e:
if NO_ORIGIN_FOR_SPA_CLIENT_ERROR in str(e):
# Retry in cases of 'Single-Page Application' client-type with
# 'Origin' header equal to client's redirect URL.
headers = {'Origin': self._client.redirect_url}
msg = f'Retrying OAuth token exchange with {self._client.redirect_url} origin'
logger.debug(msg)
continue
raise e


class Consent:
Expand Down