I am using Provide Hosted App & created a web job (Hosted in Azure)which configure my SharePoint Online site collection. This web job uses CSOM. It is working properly but after 30 minute it shows error :
Exception :The remote server returned an error: (401) Unauthorized. Server        : RD00155D31D5E0 Parent Method Name: CreateAndUploadLogFile Error Method     : GetResponse Method Tree      : GetResponse ,Execute ,ExecuteQueryToServer ,ExecuteQuery ,ExecuteQuery ,ExecuteQuery ,CreateAndUploadLogFile , Assembly Name    : BizPortal.RemoteProvisioning.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Error Type       : System.Net.WebException Message          : The remote server returned an error: (401) Unauthorized. Source           : System Target           : System.Net.WebResponse GetResponse() Stack Trace      :   at System.Net.HttpWebRequest.GetResponse() at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute() at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery() at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
It is running properly but suddenly after 30 minutes throwing this error . I am not changing authentication. I am configuring whole site collection & it is creating sites & lists but suddenly throwing errors.
Uri targetSite = new Uri(SiteCollectionUrl); //Get the realm for the URL string realm = TokenHelper.GetRealmFromTargetUrl(targetSite); //Get the access token for the URL. // Requires this app to be registered with the tenant string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,targetSite.Authority, realm).AccessToken; //Get client context with access token using (ctx = TokenHelper.GetClientContextWithAccessToken(targetSite.ToString(), accessToken)) { Call functions here & passing “ctx” object. }
Can you verify what your token is? Are you getting a valid token? Have you registered the application correctly in SPO?
Did you get any solution for this? Please share.