Search code examples
c#azuremicrosoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-teams

User lookup by user id failed in AAD


I'm trying to create MS Teams Meeting using Microsoft.Graph.Beta SDK in .NET core application. I'm using the following code to create the meeting.

 static void Main(string[] args)
        {
            var clientId = "<Enter you Client ID here>";
            var tenantId = "<Enter your tenand ID here>";
            var clientSecret = "<Enter your client secret here>";
            var scopes = new string[] { "https://graph.microsoft.com/.default" };

            ///The client credential flow enables service applications to run without user interaction. 
            ///Access is based on the identity of the application.
            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithTenantId(tenantId)
            .WithClientSecret(clientSecret)
            .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);



            var onlinemeeting = CreateTeamsMeeting(authProvider).GetAwaiter().GetResult();


            Console.ReadLine();
        }


        public static async Task<OnlineMeeting> CreateTeamsMeeting(IAuthenticationProvider authProvider)
        {
            GraphServiceClient graphClient = new GraphServiceClient(authProvider);

            var onlineMeeting = new OnlineMeeting
            {
                StartDateTime = DateTimeOffset.Parse("2020-11-12T21:30:34.2444915+00:00"),
                EndDateTime = DateTimeOffset.Parse("2020-11-12T22:00:34.2464912+00:00"),
                Subject = "User Token Meeting",
            };

            return await graphClient.Me.OnlineMeetings
                .Request()
                .AddAsync(onlineMeeting);
        }

However, upon running the above code, I receive the following error.

Server error: User lookup by user id failed in AAD. Client exception: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.

Inner error: AdditionalData: date: 2020-11-09T12:19:53 request-id: 02cd2168-d0d8-437a-9eee-117f1924a387 client-request-id: 02cd2168-d0d8-437a-9eee-117f1924a387 ClientRequestId: 02cd2168-d0d8-437a-9eee-117f1924a387

enter image description here

How do we fix this error??


Solution

  • @juusnas's answer is correct. I just organize my comments here for easy reference.

    You are using client credential flow based on Client credentials provider. So you should use graphClient.Users["{user id/upn}"].OnlineMeetings.Request().AddAsync(onlineMeeting) rather than graphClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting).

    And if you want to implement Get the token as a user, you should choose Authorization code provider or Username/password provider. Then you could keep using graphClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting).

    For permission error, you need to add Application permission (not delegated permission) OnlineMeetings.ReadWrite.All into your App registration.

    enter image description here

    And based on the Important tip here, you also must create an application access policy to allow applications to access online meetings on behalf of a user.