Microsoft Graph faces InvalidAuthenticationToken when sending big J.S.O.N. data

Tintin10 40 Reputation points
2026-07-10T13:57:04.0533333+00:00

Hello. I maintain and develop a C# application (.NET Framework 4.8) that aims at writing into Microsoft Excel files on Sharepoint, using Microsoft Graph 5.91.

For writing purposes, I need to manually build the PATCH query, since there’s no PatchAsync method inside the Microsoft.Graph.Drives.Item.Items.Item.Workbook.Worksheets.Item.RangeWithAddress.RangeWithAddressRequestBuilder.

Hence is my code below :

AccessToken accessToken = credentials.GetToken(new TokenRequestContext(new string[] { "https://graph.microsoft.com/.default" }));
log.Info($"Token expires on {accessToken.ExpiresOn}, should be refreshed on {accessToken.RefreshOn}, has type {accessToken.TokenType}.");
HttpClient customHttpClient = new HttpClient()
{
    BaseAddress = new Uri("https://graph.microsoft.com"),
    Timeout = TimeSpan.FromSeconds(timeout)
};
customHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Token);
Uri url = worksheetRequestBuilder.RangeWithAddress(rangeA1).ToGetRequestInformation().URI;
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), url);
request.Content = new StringContent($"{{'values':{valuesText},'formulas':{formulasText}{(numberFormats != null ? $",'numberFormat':{numberFormatsText}" : string.Empty)}}}", Encoding.UTF8, "application/json");
HttpResponseMessage result = await httpClient.SendAsync(request);

This code works fine, unless the J.S.O.N. string weighs too much. My case is about a J.S.O.N. string to send that weight more than 4 MB (I can even not share it here). In such a case, the following error is received : Unauthorized : {"error":{"code":"InvalidAuthenticationToken","message":"Access token is empty.","innerError":{"date":"2026-07-10T08:41:10","request-id":"XXXX","client-request-id":"XXXX"}}}

I know the token is not empty : it is proved by the log line in the code above, which results in :

Token expires on 10/07/2026 09:40:54 +00:00, should be refreshed on , has type Bearer.

This token even seems to not be refreshed and to be used by previous queries a few seconds ago.

Can you let me know of some limitations about the data to be sent, or any other bug I’m not aware about ?

Thanks in advance.

Microsoft Security | Microsoft Graph
0 comments No comments

1 answer

Sort by: Most helpful
  1. Sina Salam 30,566 Reputation points Volunteer Moderator
    2026-07-13T12:58:10.1733333+00:00

    Hello Tintin10,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that your Microsoft Graph faces InvalidAuthenticationToken when sending big J.S.O.N. data.

    The token’s expiry is not the primary issue indicated by the evidence because smaller requests succeed with the same token. However, the posted code configures the bearer token on customHttpClient and sends the request with httpClient. Correct that ambiguity first by attaching the bearer token directly to the outgoing HttpRequestMessage.

    What you can do is to:

    • Serialize the values, formulas and optional numberFormat properties with a JSON serializer.
    • Attach Authorization: Bearer {token} directly to each outgoing request.
    • Create a persistent Excel workbook session.
    • Divide the large worksheet update into smaller row-aligned rectangular ranges.
    • Send the range PATCH operations sequentially through the same workbook session.
    • Validate every response and retry only the failed range, respecting Retry-After for HTTP 429.
    • Capture the Microsoft request-id, response date, exact UTF-8 request size and successful/failing boundary if the corrected request still returns 401.

    Microsoft documents PATCH .../workbook/worksheets/{id|name}/range(address='{address}') as the supported operation for modifying range values, formulas and number formats. Microsoft also recommends omitting properties that are not being changed. - https://learn.microsoft.com/en-us/graph/api/range-update?view=graph-rest-1.0

    After the request is divided into smaller rectangular range updates, the workbook write can complete without sending the entire dataset in one oversized JSON body. There is no documented Excel-range upload-session API, so a OneDrive large-file upload session and Microsoft Graph $batch are not appropriate replacements. Graph batching only groups ordinary requests and does not convert an Excel range update into a resumable upload.

    Use the following official Microsoft resources for more reading and implementations:

    I hope this is helpful. Please! Do not hesitate to let me know if you have any other questions, steps or clarifications.


    Please do not close the thread by upvoting and accepting the answer if any part of it is helpful.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.