You can check the expiration of the token, and fetch a new before it expires.
How to manage possible Token Expiration while running long Tasks using CSOM
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.