MCPcopy Create free account
hub / github.com/mlco2/codecarbon / get_access_token

Function get_access_token

codecarbon/cli/auth.py:193–226  ·  view source on GitHub ↗

Return a valid access token, refreshing or failing with a clear message.

()

Source from the content-addressed store, hash-verified

191
192
193def get_access_token() -> str:
194 """Return a valid access token, refreshing or failing with a clear message."""
195 try:
196 creds = _load_credentials()
197 except Exception as e:
198 raise ValueError(
199 "Not able to retrieve the access token, "
200 f"please run `codecarbon login` first! (error: {e})"
201 )
202
203 access_token = creds.get("access_token")
204 if not access_token:
205 raise ValueError("No access token found. Please run `codecarbon login` first.")
206
207 # Fast path: token is still valid for the current OIDC provider
208 if _validate_access_token(access_token):
209 return access_token
210
211 # Token is expired or was issued by a different provider — try refresh
212 refresh_token = creds.get("refresh_token")
213 if refresh_token:
214 try:
215 new_tokens = _refresh_tokens(refresh_token)
216 _save_credentials(new_tokens)
217 return new_tokens["access_token"]
218 except Exception:
219 pass
220
221 # Refresh failed — credentials are stale (e.g. auth provider migrated)
222 _CREDENTIALS_FILE.unlink(missing_ok=True)
223 raise ValueError(
224 "Your session has expired or the authentication provider has changed. "
225 "Please run `codecarbon login` again."
226 )
227
228
229def get_id_token() -> str:

Callers 5

show_configFunction · 0.90
api_getFunction · 0.90
loginFunction · 0.90
get_api_keyFunction · 0.90
configFunction · 0.90

Calls 4

_load_credentialsFunction · 0.85
_validate_access_tokenFunction · 0.85
_refresh_tokensFunction · 0.85
_save_credentialsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…