Azure cognitive translator service DefaultAzureCredential authentication failed to retrieve a token.

Thirumal Rao D 1 Reputation point
2023-12-19T11:05:15.4433333+00:00

I have created Azure AI services multi-service account enabling system-managed identity and trying to execute the below python code but getting an authentication error.

I have added role-assignment permission to the user and for the service.

Please help me out to resolve an issue.

thanks in advance!

And the error is :

ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials. Attempted credentials: EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue. ManagedIdentityCredential: This credential requires exactly one scope per token request. To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.
Traceback:
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "/Users/thirumaldage/Documents/python/Trans/mytrans.py", line 129, in <module>
    translation = client.translate(text_to_translate, to=target_languages, target_language=target_language)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 76, in wrapper_use_tracer
    return func(*args, **kwargs)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/ai/translation/text/_operations/_operations.py", line 721, in translate
    pipeline_response: PipelineResponse = self._client._pipeline.run(  # pylint: disable=protected-access
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 202, in run
    return first_node.send(pipeline_request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
[Previous line repeated 2 more times]
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/policies/_redirect.py", line 156, in send
    response = self.next.send(request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/policies/_retry.py", line 448, in send
    response = self.next.send(request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/policies/_authentication.py", line 111, in send
    self.on_request(request)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/core/pipeline/policies/_authentication.py", line 90, in on_request
    self._token = self._credential.get_token(*self._scopes)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/identity/_credentials/default.py", line 225, in get_token
    token = super().get_token(*scopes, claims=claims, tenant_id=tenant_id, **kwargs)
File "/Users/thirumaldage/Documents/python/Trans/myenv/lib/python3.9/site-packages/azure/identity/_credentials/chained.py", line 123, in get_token
    raise ClientAuthenticationError(message=message)
from azure.identity import DefaultAzureCredential
from azure.ai.translation.text import TextTranslationClient, TextTranslationClient

  #Streamlit UI
st.title('Azure Text Translator')

# Initialize the default Azure credentials
credential = DefaultAzureCredential()

# Replace 'YOUR_TRANSLATOR_ENDPOINT' with your actual Translator endpoint
#endpoint = "xxxxx"
endpoint = "xxxxxxx"
#endpoint = st.text_input('Enter your Azure Translator Endpoint')

# Create an instance of the TranslationClient using the default credentials
client = TextTranslationClient(endpoint=endpoint, credential=credential)

# Text to be translated
text_to_translate = "Hello, how are you?"

# Target language code
target_language = "fr"  # Replace 'fr' with your desired target language code
target_languages = "en"

# Translate text
translation = client.translate(text_to_translate, to=target_languages, target_language=target_language)

# Output the translated text
print(f"Text '{text_to_translate}' was translated to '{target_language}': '{translation.result}'")
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,628 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.