SubtitleEdit/subtitleedit · error · Exception

Can't get Access Token

Error message

Can't get Access Token

What it means

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).

Source

Thrown at src/libuilogic/AutoTranslate/MicrosoftTranslator.cs:57

        public string Name => StaticName;
        public string Url => "https://www.bing.com/translator";
        public string Error { get; set; } = string.Empty;
        public int MaxCharacters => 1500;

        public void Initialize()
        {
            _apiKey = Configuration.Settings.Tools.MicrosoftTranslatorApiKey;
            _tokenEndpoint = Configuration.Settings.Tools.MicrosoftTranslatorTokenEndpoint;
            _category = Configuration.Settings.Tools.MicrosoftTranslatorCategory;

            try
            {
                _accessToken = GetAccessToken(_apiKey, _tokenEndpoint);
                _accessTokenFetchedUtc = DateTime.UtcNow;
            }
            catch (Exception e)
            {
                throw new Exception("Can't get Access Token", e);
            }
        }

        public List<TranslationPair> GetSupportedSourceLanguages()
        {
            return GetTranslationPairs();
        }

        public List<TranslationPair> GetSupportedTargetLanguages()
        {
            return GetTranslationPairs();
        }

        public async Task<string> Translate(string text, string sourceLanguageCode, string targetLanguageCode, CancellationToken cancellationToken)
        {
            var url = string.Format(TranslateUrl, sourceLanguageCode, targetLanguageCode);
            if (!string.IsNullOrEmpty(_category))
            {

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Get the inner exception to see the real status/body (logged by GetAccessToken via SeLogger).
  2. Verify MicrosoftTranslatorApiKey matches the Azure Translator resource's KEY 1/KEY 2.
  3. Verify MicrosoftTranslatorTokenEndpoint is the correct region-specific issueToken URL for that resource.
  4. Confirm the key's region matches the endpoint's region.
  5. Check network/proxy access to the token endpoint.

Example fix

// before
catch (Exception e)
{
    throw new Exception("Can't get Access Token", e);
}

// after - include the endpoint and status hint from the inner exception to speed diagnosis
catch (Exception e)
{
    var hint = string.IsNullOrEmpty(_apiKey) ? " No API key is configured." : $" Token endpoint: {_tokenEndpoint}";
    throw new Exception("Can't get Access Token." + hint, e);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate Azure Translator settings before Initialize
var key = Configuration.Settings.Tools.MicrosoftTranslatorApiKey;
var endpoint = Configuration.Settings.Tools.MicrosoftTranslatorTokenEndpoint;
if (string.IsNullOrWhiteSpace(key))
    throw new InvalidOperationException("Microsoft Translator API key is not set.");
if (string.IsNullOrWhiteSpace(endpoint) || !endpoint.Contains("issueToken", StringComparison.OrdinalIgnoreCase))
    throw new InvalidOperationException($"Microsoft Translator token endpoint looks invalid (should contain 'issueToken'): {endpoint}");

Type guard

public static bool IsAzureConfigured(string key, string endpoint)
    => !string.IsNullOrWhiteSpace(key)
    && !string.IsNullOrWhiteSpace(endpoint)
    && endpoint.Contains("issueToken", StringComparison.OrdinalIgnoreCase);

Try / catch

try
{
    translator.Initialize();
}
catch (Exception ex) when (ex.Message == "Can't get Access Token")
{
    var inner = ex.InnerException;
    throw new InvalidOperationException($"Failed to get Azure access token. Key set: {!string.IsNullOrWhiteSpace(key)}. Endpoint: {endpoint}. Inner: {inner?.Message}", ex);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/e0279ee8fa2f90ff. Report an issue: GitHub.