{"record":{"id":"36b767e81c4b48c7","repo":"MathewSachin/Captura","slug":"failed-to-refresh-imgur-token","errorCode":null,"errorMessage":"Failed to Refresh Imgur token","messagePattern":"Failed to Refresh Imgur token","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Captura.Imgur/ImgurUploader.cs","lineNumber":80,"sourceCode":"        }\n\n        async Task<string> GetAuthorizationHeader()\n        {\n            if (_settings.Anonymous)\n            {\n                return $\"Client-ID {_apiKeys.ImgurClientId}\";\n            }\n\n            if (string.IsNullOrWhiteSpace(_settings.AccessToken))\n            {\n                throw new Exception(\"Not logged in to Imgur\");\n            }\n\n            if (_settings.IsExpired())\n            {\n                if (!await RefreshToken())\n                {\n                    throw new Exception(\"Failed to Refresh Imgur token\");\n                }\n            }\n\n            return $\"Bearer {_settings.AccessToken}\";\n        }\n\n        async Task<bool> RefreshToken()\n        {\n            var args = new NameValueCollection\n            {\n                { \"refresh_token\", _settings.RefreshToken },\n                { \"client_id\", _apiKeys.ImgurClientId },\n                { \"client_secret\", _apiKeys.ImgurSecret },\n                { \"grant_type\", \"refresh_token\" }\n            };\n\n            using var w = new WebClient { Proxy = _proxySettings.GetWebProxy() };\n            var token = await UploadValuesAsync<ImgurRefreshTokenResponse>(w, \"https://api.imgur.com/oauth2/token.json\", args);","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/MathewSachin/Captura/blob/3fdf41529bf4d50cd7a85aedd856f30e34279a47/src/Captura.Imgur/ImgurUploader.cs#L62-L98","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-run the full OAuth authorization flow to obtain a fresh AccessToken and RefreshToken.","Verify _apiKeys.ImgurClientId and ImgurSecret match the registered Imgur application.","Check ProxySettings.GetWebProxy() is not blocking api.imgur.com.","Ensure system clock is correct so ExpiresAt comparisons are reliable."],"exampleFix":"// before\nif (!await RefreshToken()) throw new Exception(\"Failed to Refresh Imgur token\");\n// after\nif (!await RefreshToken())\n    throw new ImgurReauthRequiredException(); // caller triggers full OAuth flow","handlingStrategy":"retry","validationCode":"// verify the refresh materials are present before attempting\nif (string.IsNullOrWhiteSpace(_settings.RefreshToken)\n    || string.IsNullOrWhiteSpace(_apiKeys.ImgurSecret))\n    throw new ImgurReauthRequiredException();","typeGuard":null,"tryCatchPattern":"try { await uploader.Upload(img, fmt, progress); }\ncatch (Exception e) when (e.Message.Contains(\"Failed to Refresh\")) { await StartFullOAuthFlow(); /* one retry */ }","preventionTips":["Re-run full OAuth (not just refresh) when RefreshToken returns false.","Verify _apiKeys.ImgurClientId/ImgurSecret match the registered app.","Ensure the proxy permits api.imgur.com and the system clock is accurate."],"tags":["imgur","oauth","token-refresh","authentication","csharp"],"backgroundTag":null,"analyzedSha":"3fdf41529bf4d50cd7a85aedd856f30e34279a47","analyzedAt":"2026-08-13T19:35:27.486Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}