Skip to content

Commit 0278c6d

Browse files
author
Ing. Fabian Steiner Bsc.
committed
Refine OAuth token endpoint handling by preserving regional subdomains and updating comments for clarity
1 parent 11270de commit 0278c6d

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Core: support OAuth client credentials authentication via `client_id` and `client_secret` in `XurrentApiHelper` while maintaining API key compatibility.
13-
- Core: OAuth token endpoint TLD is now dynamically derived from the API base URL, ensuring the same top-level domain is used for both API and OAuth.
13+
- Core: The OAuth token endpoint now dynamically determines the domain from `base_url`, preserving any regional subdomains to ensure consistency between API and OAuth endpoints.
1414
- Core: When using OAuth, if a 401 Unauthorized error is received, the token is automatically refreshed and the API call is retried once. If authentication still fails after token refresh, an explicit HTTPError is raised.
1515

1616
## [0.10.0] - 2025-08-16

src/xurrent/core.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,24 @@ def _ensure_access_token(self):
117117

118118
def _obtain_access_token(self):
119119
"""Fetch a new OAuth access token using the client credentials grant."""
120-
# Dynamically determine the TLD from the base_url
120+
# Dynamically determine the domain from the base_url and preserve regional subdomains
121121
import urllib.parse
122122
parsed = urllib.parse.urlparse(self.base_url)
123-
# Extract the netloc (e.g. api.xurrent.com) and replace the subdomain with 'oauth'
123+
# Extract the netloc (e.g. api.xurrent.com, api.au.xurrent.com) and replace 'api' with 'oauth'
124124
netloc_parts = parsed.netloc.split('.')
125125
if len(netloc_parts) < 2:
126-
raise ValueError('Invalid base_url for extracting TLD')
127-
tld = '.'.join(netloc_parts[-2:])
128-
token_url = f'https://oauth.{tld}/token'
126+
raise ValueError('Invalid base_url for extracting domain')
127+
128+
# Replace the first part (assumed to be 'api') with 'oauth'
129+
if netloc_parts[0] == 'api':
130+
netloc_parts[0] = 'oauth'
131+
else:
132+
self.logger.warning(f"Expected first domain part to be 'api', got '{netloc_parts[0]}'. Proceeding anyway.")
133+
netloc_parts[0] = 'oauth'
134+
135+
# Reconstruct the domain preserving all parts including regional subdomains
136+
oauth_domain = '.'.join(netloc_parts)
137+
token_url = f'https://{oauth_domain}/token'
129138
payload = {
130139
'client_id': self._client_id,
131140
'client_secret': self._client_secret,

0 commit comments

Comments
 (0)