Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Get user Display Name from ID #1056

Open
rumethj opened this issue Jan 9, 2025 · 0 comments
Open

Get user Display Name from ID #1056

rumethj opened this issue Jan 9, 2025 · 0 comments
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned

Comments

@rumethj
Copy link

rumethj commented Jan 9, 2025

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
@rumethj rumethj added the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Jan 9, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned
Projects
None yet
Development

No branches or pull requests

1 participant