How to manage possible Token Expiration while running long Tasks using CSOM

Alvaro Avila 165 Reputation points
2023-08-23T14:32:19.0633333+00:00

On my App I'm getting the access token using the MSAL package and then add it to the context like sample below:

using var clientContext = new ClientContext(oSite.Url);
            clientContext.ExecutingWebRequest += (sender, e) =>
            {
                e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + rootSiteAccessToken;
            };

with this context then I get the files on each library and iterate through them make the needed changes.

At the moment I got stuck on getting the files; after 500k items I got access denied. I imagine this is due the token expiration. I'm doing more test for confirming this scenario, but I will definitely have this issue at some moment because the main library has 3M files. Although I'm getting the files and processing them by batches to don't run out of memory, I expect the Task will need to run for few days.

I wonder, which would be the best practice on this situation to not falling on access denied due Token expiration.

Using PnP PowerShell I have run scripts that run for weeks. But I haven't been able to get clarity from its code on how they manage the interactive credentials.

Also, if anyone has a good recommendation on how to collect 3M files efficiently it would be awesome.

Many Thanks.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,404 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,296 Reputation points
    2023-08-23T15:22:16.2466667+00:00

    You can check the expiration of the token, and fetch a new before it expires.


Your answer

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