Hello Lilu Wan,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that your mssparkutils.credentials.getToken() does not support CognitiveServices or AIServices audience aliases and you need away to acquire SAMI token for Azure OpenAI or Azure AI Foundry from Spark notebook.
I reviewed this against the current Microsoft documentation, and the issue is a product limitation, not a misconfiguration. In Azure Synapse Spark, mssparkutils.credentials.getToken() only supports a fixed, documented set of audiences, and Azure OpenAI / Azure AI Foundry audiences are not in that supported list today. That is why values such as CognitiveServices, AIServices, AzureOpenAI, AzureAIServices, https://cognitiveservices.azure.com, and https://ai.azure.com do not work from the notebook. - https://learn.microsoft.com/en-us/azure/synapse-analytics/spark/apache-spark-secure-credentials-with-tokenlibrary, https://learn.microsoft.com/en-us/azure/synapse-analytics/spark/microsoft-spark-utilities
There is also a second documented limitation: in Synapse notebooks and Spark job definitions, the workspace system-assigned managed identity is only supported through linked services and mssparkutils APIs, and MSAL / other authentication libraries cannot use that SAMI path inside the Spark notebook runtime. Because of that, switching to ManagedIdentityCredential, DefaultAzureCredential, or MSAL inside the Synapse notebook is not a supported fix for this exact scenario. - https://learn.microsoft.com/en-us/azure/synapse-analytics/synapse-service-identity, https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.managedidentitycredential?view=azure-python, https://learn.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python
So, my best advice since there is no supported way today to obtain an Azure OpenAI / Azure AI Foundry token directly from a Synapse Spark notebook by using the workspace system-assigned managed identity. Adding RBAC such as Cognitive Services OpenAI User / Contributor or Foundry roles is necessary for authorization, but it does not solve this case because the failure happens earlier, at token acquisition. - https://learn.microsoft.com/en-us/dotnet/ai/how-to/app-service-aoai-auth, https://learn.microsoft.com/en-us/azure/foundry/concepts/authentication-authorization-foundry, https://learn.microsoft.com/en-us/azure/synapse-analytics/spark/apache-spark-secure-credentials-with-tokenlibrary
If the requirement is to remain secretless and managed-identity-based, the only reliable and supportable design is to move the Azure OpenAI / Azure AI Foundry call to an Azure host that supports managed identity directly, such as Azure Functions, App Service, Container Apps, AKS, or VM, assign the required AI roles there, and call that service from Synapse. Microsoft documents that model for Azure-hosted applications using managed identity with Azure OpenAI, and Foundry documentation recommends Microsoft Entra ID for production workloads. - https://learn.microsoft.com/en-us/dotnet/ai/how-to/app-service-aoai-auth, https://learn.microsoft.com/en
If the requirement is specifically “keep the call inside Synapse Spark and use workspace SAMI directly”, then this remains an unsupported scenario and should be treated as a feature gap in Synapse TokenLibrary / Token Service, not as a notebook-level fix. There is a supported Foundry Tools linked service path in Synapse, but the documented setup uses Key Vault-stored keys, so it changes the authentication model and does not meet your original “SAMI-only / no-secrets” requirement. - https://learn.microsoft.com/en-us/azure/synapse-analytics/synapse-service-identity, https://learn.microsoft.com/en-us/azure/synapse-analytics/machine-learning/tutorial-configure-cognitive-services-synapse
I hope this is helpful! Do not hesitate to let me know if you have any other questions, steps or clarifications.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.