I am using AzureAd along with the Microsoft Graph Client to do calculations on Excel spreadsheets. It works most of the time but every now and again the request comes back with a Invalid Session error:
Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: InvalidSession
Message: The target session is invalid.
Inner error:
Code: invalidSessionReCreatable
Message: The session specified in the request does not exist or is invalid due to a transient error.
Inner error:
Code: InvalidOrTimedOutSession
Message: We noticed that you haven't been interacting with this workbook, so we paused your session.
It seems like the session gets paused before the API gets to finish the call (which happens in less than a second).
Startup Code to configure AD and Graph Services:
services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(Configuration.GetSection("GraphBeta"))
.AddInMemoryTokenCaches();
Creating the session:
WorkbookSessionInfo res = await _graphServiceClient.Groups[_groupId].Drive.Items[productStructureCalculator.CalculatorId].Workbook
.CreateSession(persistChanges)
.Request()
.Header("Prefer", "respond-async")
.PostAsync();
Doing a batch request:
var batchRequestInfo = CreateBatchRequestAsync(sessionId, structure, productStructureCalculator, productMatrixInputs);
var resultRequest = _graphServiceClient.Groups[_groupId].Drive.Items[productStructureCalculator.CalculatorId].Workbook.Worksheets[productStructureCalculator.SheetName]
.Range("D6:J6")
.Request()
.Header("workbook-session-id", "{" + sessionId + "}")
.Header("Prefer", "return=minimal")
.GetHttpRequestMessage();
resultRequest.Method = HttpMethod.Get;
var resultRequestID = Guid.NewGuid().ToString();
var batchResultRequestStep = new BatchRequestStep(
resultRequestID,
resultRequest,
new List<string> { batchRequestInfo.LastEventRequestId }
);
batchRequestInfo.BatchRequestContent.AddBatchRequestStep(batchResultRequestStep);
var returnedResponse = await _graphServiceClient.Batch.Request().PostAsync(batchRequestInfo.BatchRequestContent);