-
Notifications
You must be signed in to change notification settings - Fork 84
Description
A script I'm working on which is running via a container app job, requires me to resolve the identities of users from their ID to their display name. I am using a Managed Identity Credential to authenticate. The MI has been given required AD permissions Microsoft Graph: User.ReadBasic.All
I get this kind of log error: "Log":"WARNING AppServiceCredential.get_token failed: HTTP transport has already been closed. You may check if you're calling a function outside of the \u0060async with\u0060 of your client creation, or if you called \u0060await close()\u0060 on your client already."}
Could the problem be that I am running the get_user_name_from_identity() method asynchronously? Or is it something to do with the credentials?
This is the code snippet:
# Fetch the user name or display name for a given identity using Microsoft Graph API.
async def get_user_name_from_identity(identity, credential):
scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credentials=credential, scopes=scopes)
try:
user = await graph_client.users.by_user_id(identity).get()
logger.debug(f"User name for identity {identity}: {user.display_name}")
logger.debug(f"Name for identity {identity}: {user.given_name} {user.surname}")
except Exception as e:
logger.warning(f"Error getting user name for identity {identity}: {e}")
exit(1)
return identity
return user.display_name
# Resolve identities in the data using Microsoft Graph API
async def resolve_identities(data, credential):
# Extract unique identities from the data
unique_identities = {entry["Identity"] for entry in data if entry["Identity"]}
# Map each identity to a user name
identity_map = {
identity: user_name
for identity, user_name in zip(
unique_identities,
await asyncio.gather(*[get_user_name_from_identity(identity, credential) for identity in unique_identities])
)
}
# Update the data with resolved user names
for entry in data:
entry["Identity"] = identity_map.get(entry["Identity"], entry["Identity"])
return data