{"record":{"id":"e0279ee8fa2f90ff","repo":"SubtitleEdit/subtitleedit","slug":"can-t-get-access-token","errorCode":null,"errorMessage":"Can't get Access Token","messagePattern":"Can't get Access Token","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/libuilogic/AutoTranslate/MicrosoftTranslator.cs","lineNumber":57,"sourceCode":"        public string Name => StaticName;\n        public string Url => \"https://www.bing.com/translator\";\n        public string Error { get; set; } = string.Empty;\n        public int MaxCharacters => 1500;\n\n        public void Initialize()\n        {\n            _apiKey = Configuration.Settings.Tools.MicrosoftTranslatorApiKey;\n            _tokenEndpoint = Configuration.Settings.Tools.MicrosoftTranslatorTokenEndpoint;\n            _category = Configuration.Settings.Tools.MicrosoftTranslatorCategory;\n\n            try\n            {\n                _accessToken = GetAccessToken(_apiKey, _tokenEndpoint);\n                _accessTokenFetchedUtc = DateTime.UtcNow;\n            }\n            catch (Exception e)\n            {\n                throw new Exception(\"Can't get Access Token\", e);\n            }\n        }\n\n        public List<TranslationPair> GetSupportedSourceLanguages()\n        {\n            return GetTranslationPairs();\n        }\n\n        public List<TranslationPair> GetSupportedTargetLanguages()\n        {\n            return GetTranslationPairs();\n        }\n\n        public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode, CancellationToken cancellationToken)\n        {\n            var url = string.Format(TranslateUrl, sourceLanguageCode, targetLanguageCode);\n            if (!string.IsNullOrEmpty(_category))\n            {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/AutoTranslate/MicrosoftTranslator.cs#L39-L75","documentation":"Thrown by MicrosoftTranslator.Initialize() when GetAccessToken fails for any reason. The original exception is wrapped as the inner exception. Initialize is called before any translation, so this surfaces at setup time. The underlying failure is almost always auth/endpoint configuration (see error 55 for the wrapped throw).","triggerScenarios":"Initialize() reads MicrosoftTranslatorApiKey, MicrosoftTranslatorTokenEndpoint, MicrosoftTranslatorCategory from config and calls GetAccessToken; GetAccessToken POSTs the key as the Ocp-Apim-Subscription-Key header to the token endpoint; any failure there is caught and rethrown with this message.","commonSituations":"Azure Translator key not set or wrong format; token endpoint URL wrong (must be region-specific, e.g. https://api.cognitive.microsoft.com/sts/v1.0/issueToken for global, or a regional endpoint); no network; key from a different region than the endpoint; key revoked.","solutions":["Get the inner exception to see the real status/body (logged by GetAccessToken via SeLogger).","Verify MicrosoftTranslatorApiKey matches the Azure Translator resource's KEY 1/KEY 2.","Verify MicrosoftTranslatorTokenEndpoint is the correct region-specific issueToken URL for that resource.","Confirm the key's region matches the endpoint's region.","Check network/proxy access to the token endpoint."],"exampleFix":"// before\ncatch (Exception e)\n{\n    throw new Exception(\"Can't get Access Token\", e);\n}\n\n// after - include the endpoint and status hint from the inner exception to speed diagnosis\ncatch (Exception e)\n{\n    var hint = string.IsNullOrEmpty(_apiKey) ? \" No API key is configured.\" : $\" Token endpoint: {_tokenEndpoint}\";\n    throw new Exception(\"Can't get Access Token.\" + hint, e);\n}","handlingStrategy":"validation","validationCode":"// Validate Azure Translator settings before Initialize\nvar key = Configuration.Settings.Tools.MicrosoftTranslatorApiKey;\nvar endpoint = Configuration.Settings.Tools.MicrosoftTranslatorTokenEndpoint;\nif (string.IsNullOrWhiteSpace(key))\n    throw new InvalidOperationException(\"Microsoft Translator API key is not set.\");\nif (string.IsNullOrWhiteSpace(endpoint) || !endpoint.Contains(\"issueToken\", StringComparison.OrdinalIgnoreCase))\n    throw new InvalidOperationException($\"Microsoft Translator token endpoint looks invalid (should contain 'issueToken'): {endpoint}\");","typeGuard":"public static bool IsAzureConfigured(string key, string endpoint)\n    => !string.IsNullOrWhiteSpace(key)\n    && !string.IsNullOrWhiteSpace(endpoint)\n    && endpoint.Contains(\"issueToken\", StringComparison.OrdinalIgnoreCase);","tryCatchPattern":"try\n{\n    translator.Initialize();\n}\ncatch (Exception ex) when (ex.Message == \"Can't get Access Token\")\n{\n    var inner = ex.InnerException;\n    throw new InvalidOperationException($\"Failed to get Azure access token. Key set: {!string.IsNullOrWhiteSpace(key)}. Endpoint: {endpoint}. Inner: {inner?.Message}\", ex);\n}","preventionTips":["Set both MicrosoftTranslatorApiKey and the region-correct MicrosoftTranslatorTokenEndpoint in settings.","Match the key's region to the endpoint's region.","Do a token-fetch health check on Initialize to fail fast with a clear cause.","Inspect the inner exception (and the SeLogger line from GetAccessToken) for Azure's status/body."],"tags":["api","auth","azure","translation","configuration"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}