microsoftgraph / msgraph-sdk-dotnet-auth

Archived - use the TokenCredential classes provided by Azure.Identity. https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme
https://graph.microsoft.com
MIT License
78 stars 19 forks source link

How to solve “Microsoft.graph.serviceexception code "generalException" an error occurred sending the request” while fetching calendar events? #96

Closed stefan1st closed 3 years ago

stefan1st commented 3 years ago

I have created an auth provider using user-password auth provider and trying to retrieve calendar events in bot code which is in c#.

Inner Exception :

Error={Code: generalException Message: Unexpected exception returned from MSAL. }

Inner Exception Message:

A configuration issue is preventing authentication - check the error message from the server for details. You can modify the configuration in the application registration portal. See https://aka.ms/msal-net-invalid-client for details.  Original exception: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.
Trace ID: 843e13e1-dac2-45e6-a2c3-875359892000
Correlation ID: 7086f4f8-acca-4090-804a-6a44ab48f162
Timestamp: 2021-01-04 10:44:59Z

StackTrace :

at Microsoft.Graph.HttpProvider.<SendRequestAsync>d__19.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.HttpProvider.<SendAsync>d__18.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__38.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.Graph.UserRequest.<GetAsync>d__6.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Gerald.Api.Service.Services.EventsService.<FindMeetingTime>d__10.MoveNext() in ...EventsService.cs:line 249

My Code:

`var attendees = new List() { new AttendeeBase { EmailAddress = new EmailAddress { Address = "gerald@Hughes.eu", Name = "Gerald Hughes" }, Type = AttendeeType.Required } };

var timeConstraint = new TimeConstraint
{
    TimeSlots = new List<TimeSlot>()
    {
        new TimeSlot
        {
            Start = new DateTimeTimeZone
            {
                DateTime = "2020-12-31T09:02:35.999Z",
                TimeZone = "Pacific Standard Time"
            },
            End = new DateTimeTimeZone
            {
                DateTime = "2020-12-31T20:02:35.999Z",
                TimeZone = "Pacific Standard Time"
            }
        }
    }
};

var locationConstraint = new LocationConstraint
{
    IsRequired = false,
    SuggestLocation = true,
    Locations = new List<LocationConstraintItem>()
    {
        new LocationConstraintItem
        {
            DisplayName = "Conf Room 32/1368",
            LocationEmailAddress = "conf32room1368@imgeek.onmicrosoft.com"
        }
    }
};

var meetingDuration = new Duration("PT1H");

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
                .Create(_microsoftAppId)
                .WithTenantId(_microsoftAppTenantId)
                .WithRedirectUri(_redirectUrl)
                .Build();            

var scopes = new List<string>
{
    "Calendars.Read",
    "Calendars.Read.Shared",
    "Calendars.ReadWrite",
    "Calendars.ReadWrite.Shared",
    "ChannelMessage.Send",
    "openid",
    "People.Read",
    "People.Read.All",
    "profile",
    "User.Read",
    "User.ReadBasic.All",
    "email"
};
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);

_graphServiceClient = new GraphServiceClient(authProvider);

SecureString theSecureString = new NetworkCredential("", "secret123!").SecurePassword;

var me = await _graphServiceClient.Me.Request()
                     .WithUsernamePassword("gerald@Hughes.eu", theSecureString).GetAsync();

var result = await _graphServiceClient.Me
    .FindMeetingTimes(attendees,
        locationConstraint,
        timeConstraint,
        meetingDuration,
        null,
        null,
        null,
        null)
    .Request().WithUsernamePassword("gerald@Hughes.eu", theSecureString)
    .PostAsync();` 

AB#7216

pschaeflein commented 3 years ago

As hinted in the error message, the configuration of the application in the AAD portal is not correct.

https://www.schaeflein.net/aadsts7000218-public-client/

stefan1st commented 3 years ago

As hinted in the error message, the configuration of the application in the AAD portal is not correct.

https://www.schaeflein.net/aadsts7000218-public-client/

Well, I've tried this, and I have the same error. Am I missing something? image

pschaeflein commented 3 years ago

Not sure what else without diving into code. I can say that Username/Password is not really the best choice anymore. I would use the Interactive or DeviceCode providers instead. There are samples in the Graph.Community library.

Also, the Bot Framework allows for getting a token thru an OAuth dialog or using single sign-on.

MIchaelMainer commented 3 years ago

+1 to what Paul said.

The username password flow doesn't use a redirect URI. Try removing it.

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
                .Create(_microsoftAppId)
                .WithTenantId(_microsoftAppTenantId)
                //.WithRedirectUri(_redirectUrl)
                .Build();      

Also, check from your app registration that public applications are enabled.

image

Are you able to successfully use this flow?

ghost commented 3 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.