Skip to content

Fix inverted refresh margin in KaggleCredentials.access_token_has_expired - #77

Open
kohbanye wants to merge 1 commit into
Kaggle:mainfrom
kohbanye:fix-access-token-refresh-margin
Open

kohbanye wants to merge 1 commit into
Kaggle:mainfrom
kohbanye:fix-access-token-refresh-margin

Conversation

@kohbanye

@kohbanye kohbanye commented Oct 26, 2026 •

Copy link
Copy Markdown

Summary

KaggleCredentials.access_token_has_expired() subtracts its 30-minute margin from the current time:

self._access_token_expiration < datetime.now(timezone.utc) - timedelta(minutes=30)

This is true only once the token has been expired for more than 30 minutes. For the first 30 minutes after expiry, get_access_token() keeps returning the expired token. Every API call then fails with HTTP 401, and the kaggle CLI prints "Authentication required to call the Kaggle API." Meanwhile kaggle auth login reports "You are already logged-in". The failure clears itself 30 minutes later.

This change adds the margin instead, so the token is refreshed 30 minutes before it expires. The margin is now a named constant, ACCESS_TOKEN_REFRESH_MARGIN.

Needs manual sync with the kapigen base.

Fixes #74. Also reported in Kaggle/kaggle-cli#1217.

Verification

Ran kaggle competitions list with kaggle 2.2.4 against the live API, using a ~/.kaggle/credentials.json whose access_token_expiration was set to 10 minutes in the past:

  • Before this change: the CLI printed "Authentication required to call the Kaggle API." and the credentials file was left unchanged.
  • After this change: the access token was refreshed (the new access_token_expiration is 12 hours later) and the command printed the competition list.

@google-cla

google-cla Bot commented Oct 26, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@kohbanye
kohbanye force-pushed the fix-access-token-refresh-margin branch from dccfde7 to 4080300 Compare October 26, 2026 09:09
access_token_has_expired() subtracted its 30-minute margin from the
current time, so a token counted as expired only 30 minutes after its
expiration. During that window get_access_token() returned the expired
token and every API call failed with 401.

Add the margin instead, so the token is refreshed 30 minutes before it
expires.

Fixes Kaggle#74
@kohbanye
kohbanye force-pushed the fix-access-token-refresh-margin branch from 4080300 to fe71d9a Compare October 26, 2026 09:12
@kohbanye
kohbanye marked this pull request as ready for review October 26, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KaggleCredentials.access_token_has_expired() reports a token as expired only 30 minutes after it expires

1 participant