How can you get the logged in user in a Teams App chat bot

Andrew Kochie 20 Reputation points
2024-11-28T19:07:59.7866667+00:00

Hello,

I am developing a Teams app chatbot and need guidance on the best way to retrieve the logged-in user’s information to personalize responses. The app is hosted on Azure App Service and is registered with Microsoft Graph. I have the tenant ID, client ID, client secret, and other necessary credentials.

However, I’m encountering issues with authentication to the Graph API to retrieve user profile details. Here’s what I’ve tried so far:

  1. Device Code Flow: This allows me to give consent through the browser, but the token request fails with an error indicating a missing client secret, despite it being included in the header.
  2. On-Behalf-Of Flow: I find this confusing because I’m unclear about where to obtain the initial authorization token required as the assertion in the access token request.
  3. MSAL Python Library: Using the ConfidentialClientApplication class, I can successfully obtain a token, but this token doesn’t work for hitting the /me endpoint, as it’s not a delegated flow.

Ideally, I’d like to leverage the fact that users are already signed into Teams via enterprise SSO to gain authorization to access Graph. However, I’m open to other suggestions. Is the Graph API even necessary for retrieving basic user details like a username, or is there a simpler approach?

Thank you in advance for your help!

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,968 questions
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Authenticator
Microsoft Teams | Microsoft Teams for business | Other
0 comments No comments
{count} votes

Accepted answer
  1. Azizkhon Ishankhonov 540 Reputation points
    2024-11-28T19:57:16.8666667+00:00

    Hi

    You can user your Entra Id application in order to get information about user. But, it requires wide range API permission.

    1. First, get access token on behalf of application https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow#first-case-access-token-request-with-a-shared-secret
    2. Use this access token for Graph API request to retrieve data about user.
      https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#example-1-standard-users-request
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Raja Pothuraju 23,790 Reputation points Microsoft External Staff Moderator
    2024-11-29T05:31:36.18+00:00

    Hello @Andrew Kochie,

    Thank you for posting your query on Microsoft Q&A.

    Based on your description, it seems you want to fetch user information for individuals logged into your Teams app chatbot. You can achieve this by making a Microsoft Graph API call to retrieve the signed-in user's details. To do this, you’ll need to generate an access token for the Microsoft Graph API using scopes such as user.read, email, profile, openid, and offline_access through the OAuth flow.

    Using the user.read scope, user fetch their own details by sending a GET request to the following endpoint: GET https://graph.microsoft.com/v1.0/me

    To generate a delegated access token, you can follow the steps outlined in the Microsoft documentation for the OAuth 2.0 authorization code grant flow:

    Request an ID token or hybrid flow using OAuth 2.0

    Graph API - Get user details

    If you would like to fetch details of other users, you will need to use the user.read.all scope when generating an access token. This scope allows you to retrieve information about all users within the directory.

    I hope this information is helpful. Please feel free to reach out if you have any further questions.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Thanks,
    Raja Pothuraju.


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.