MS Graph client - Code: InvalidAuthenticationToken Message: Access token has expired or is not yet valid.

Subramanya Yash 6 Reputation points
2021-09-13T13:02:21.373+00:00

Working on creating an appointment. After a couple of hours of application on the browser. it starts throwing an exception as

Code: InvalidAuthenticationToken
Message: Access token has expired or is not yet valid.

The same will be thrown even after logout the session and login in again. I need to restart the azure app service to get it worked. Please help here to resolve this issue or let me know if I am missing anything.
Below is my code:

IConfidentialClientApplication app = MsalAppBuilder.BuildConfidentialClientApplication();
AuthenticationResult result = null;
var account = await app.GetAccountAsync(ClaimsPrincipal.Current.GetAccountId());
string[] scopes = { "User.Read" };
result = await app.AcquireTokenSilent(scopes, account).ExecuteAsync().ConfigureAwait(false);

            GraphServiceClient graphClient = new GraphServiceClient(
                 new DelegateAuthenticationProvider(
                   (requestMessage) =>
                   {
                       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", result.AccessToken);
                       return Task.FromResult(0);
                   }));


            Calendar calendar = new Calendar();
            try
            {
                calendar = await graphClient.Me.Calendar.Request().GetAsync();
            }
            catch (Exception ex)
            {
                calendar = await graphClient.Users["loggedin user email"].Calendar.Request().GetAsync();
            }
            TimeZone curTimeZone = TimeZone.CurrentTimeZone;
            var curTimeZoneName = "";
                curTimeZoneName = "UTC";

            var attendees = new List<Attendee>();

            attendees.Add(new Attendee
            {
                EmailAddress = new EmailAddress
                {
                    Address = "<attendee email>",
                    Name = "<attendee name>"
                },
                Type = AttendeeType.Required
            });

            var @event = new Event
            {
                Subject = "Test appointment ",
                Body = new ItemBody
                {
                    ContentType = BodyType.Html,
                    Content = "Test appointment "
                },
                Start = new DateTimeTimeZone
                {
                    DateTime = DateTime.Now.ToUniversalTime().ToString(),
                    TimeZone = "UTC", 
                },
                End = new DateTimeTimeZone
                {
                    DateTime = DateTime.Now.AddMinutes(15).ToUniversalTime().ToString(),
                    TimeZone = "UTC", 
                },
                Location = new Microsoft.Graph.Location
                {
                    DisplayName = "Teams Call"
                },
                Attendees = attendees,
                TransactionId = Guid.NewGuid().ToString(), 
                IsOnlineMeeting = true,
                OnlineMeetingProvider = OnlineMeetingProviderType.TeamsForBusiness
            };

try
{
var eventData = await graphClient.Me.Calendars[calendar.Id].Events
.Request()
.AddAsync(@event);
}
catch (Exception ex)
{
var eventData = await graphClient.Users["loggedin user email"].Calendars[calendar.Id].Events
.Request()
.AddAsync(@event);

            }
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,212 questions
0 comments No comments
{count} vote