MathewSachin/Captura · error · Exception

Failed to Refresh Imgur token

Error message

Failed to Refresh Imgur token

What it means

Thrown by ImgurUploader.GetAuthorizationHeader (src/Captura.Imgur/ImgurUploader.cs:80) when _settings.IsExpired() is true and RefreshToken() (src/Captura.Imgur/ImgurUploader.cs:87-107) returned false. RefreshToken returns false only when the token endpoint response has an empty AccessToken, i.e. Imgur refused the refresh (wrong/revoked refresh_token or client_secret, or a network failure that returned an empty body).

Source

Thrown at src/Captura.Imgur/ImgurUploader.cs:80

        }

        async Task<string> GetAuthorizationHeader()
        {
            if (_settings.Anonymous)
            {
                return $"Client-ID {_apiKeys.ImgurClientId}";
            }

            if (string.IsNullOrWhiteSpace(_settings.AccessToken))
            {
                throw new Exception("Not logged in to Imgur");
            }

            if (_settings.IsExpired())
            {
                if (!await RefreshToken())
                {
                    throw new Exception("Failed to Refresh Imgur token");
                }
            }

            return $"Bearer {_settings.AccessToken}";
        }

        async Task<bool> RefreshToken()
        {
            var args = new NameValueCollection
            {
                { "refresh_token", _settings.RefreshToken },
                { "client_id", _apiKeys.ImgurClientId },
                { "client_secret", _apiKeys.ImgurSecret },
                { "grant_type", "refresh_token" }
            };

            using var w = new WebClient { Proxy = _proxySettings.GetWebProxy() };
            var token = await UploadValuesAsync<ImgurRefreshTokenResponse>(w, "https://api.imgur.com/oauth2/token.json", args);

View on GitHub (pinned to 3fdf41529b)

Solutions

  1. Re-run the full OAuth authorization flow to obtain a fresh AccessToken and RefreshToken.
  2. Verify _apiKeys.ImgurClientId and ImgurSecret match the registered Imgur application.
  3. Check ProxySettings.GetWebProxy() is not blocking api.imgur.com.
  4. Ensure system clock is correct so ExpiresAt comparisons are reliable.

Example fix

// before
if (!await RefreshToken()) throw new Exception("Failed to Refresh Imgur token");
// after
if (!await RefreshToken())
    throw new ImgurReauthRequiredException(); // caller triggers full OAuth flow
Defensive patterns

Strategy: retry

Validate before calling

// verify the refresh materials are present before attempting
if (string.IsNullOrWhiteSpace(_settings.RefreshToken)
    || string.IsNullOrWhiteSpace(_apiKeys.ImgurSecret))
    throw new ImgurReauthRequiredException();

Try / catch

try { await uploader.Upload(img, fmt, progress); }
catch (Exception e) when (e.Message.Contains("Failed to Refresh")) { await StartFullOAuthFlow(); /* one retry */ }

Prevention

When it happens

Trigger: An expired access token whose refresh fails: POST to https://api.imgur.com/oauth2/token.json with refresh_token/client_id/client_secret returned a body whose AccessToken is null/empty. Caused by a revoked or rotated refresh_token, mismatched client_secret, or a transport error.

Common situations: Refresh token used past Imgur's validity or revoked by the user re-logging in elsewhere; client_secret in IImgurApiKeys is wrong/rotated; proxy or network blocked the token endpoint; clock skew made IsExpired fire repeatedly.

Related errors


AI-assisted analysis of MathewSachin/Captura@3fdf41529b (2026-08-13). Data as JSON: /api/errors/36b767e81c4b48c7. Report an issue: GitHub.