Forum Discussion
Workload Identity Federation
Hi V-yubandi-msft / tayloramy ,
I am trying to run below code from my function app to connect to sharepoint using WIF , but i am getting below error , can you let me know if this is correct way or not , if not please let me know the changes.
raise CredentialUnavailableError(error_message) from ex
azure.identity._exceptions.CredentialUnavailableError: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint.
Code
from azure.identity import ManagedIdentityCredential, ClientAssertionCredential
import requests
# Constants
MI_AUDIENCE = "api://AzureADTokenExchange"
GRAPH_SCOPE = "https://graph.microsoft.com/.default"
# Managed Identity credential (USMI client ID)
managed_identity_credential = ManagedIdentityCredential(
client_id="<clinet-id>"
)
# Function to get token from MI
def get_managed_identity_token(credential, audience):
return credential.get_token(audience).token
# ✅ Correct: Pass the lambda as the third positional argument
client_assertion_credential = ClientAssertionCredential(
"", # tenant_id
"", # client_id
lambda: get_managed_identity_token(
managed_identity_credential, f"{MI_AUDIENCE}/.default"
)
)
# Function to get access token
def get_graph_token():
token = client_assertion_credential.get_token(GRAPH_SCOPE)
return token.token
# Function to call SharePoint via Microsoft Graph
def call_sharepoint():
access_token = get_graph_token()
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/json"
}
url = "https://graph.microsoft.com/v1.0/sites/y243c.sharepoint.com:/sites/SPNSite"
resp = requests.get(url, headers=headers)
if resp.status_code == 200:
print("✅ SharePoint site info retrieved successfully:")
print(resp.json())
else:
print(f"❌ Failed to retrieve SharePoint site info. Status code: {resp.status_code}")
print(resp.text)
# Run the function
if __name__ == "__main__":
call_sharepoint()Hi yashaswi_raj ,
Thank you for providing the details.
The error message:
CredentialUnavailableError: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint.
typically indicates that your Function App cannot connect to the Managed Identity service (IMDS). This may occur if, The Managed Identity is not enabled on your Function App, or code is being executed locally instead of within Azure.
Here are a few things to check
1. Ensure the System assigned Managed Identity is enabled for your Function App
2. Run the code within the Function App in Azure, not locally, since IMDS is only available to Azure resources.
3. If you are using a system assigned identity, remove the client_id parameter, as it is only required for a user assigned identity.
Helpful Reference : Managed identities for Azure resources - Managed identities for Azure resources | Microsoft Learn
Thank you for your patience. Hope this help.