An API that connects multiple Microsoft services, enabling data access and automation across platforms
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,formulasand optionalnumberFormatproperties 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-Afterfor 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:
- Update an Excel range
- Excel API best practices
- Working with Excel in Microsoft Graph
- Microsoft Graph throttling guidance
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.