ChainStoreWeb.SharePointAcsContext.RenewAccessTokenIfNeeded C# (CSharp) Method

RenewAccessTokenIfNeeded() private static method

Renews the access token if it is not valid.
private static RenewAccessTokenIfNeeded ( DateTime>.Tuple &accessToken, Func tokenRenewalHandler ) : void
accessToken DateTime>.Tuple The access token to renew.
tokenRenewalHandler Func The token renewal handler.
return void
        private static void RenewAccessTokenIfNeeded(ref Tuple<string, DateTime> accessToken, Func<OAuth2AccessTokenResponse> tokenRenewalHandler)
        {
            if (IsAccessTokenValid(accessToken))
            {
                return;
            }

            try
            {
                OAuth2AccessTokenResponse oAuth2AccessTokenResponse = tokenRenewalHandler();

                DateTime expiresOn = oAuth2AccessTokenResponse.ExpiresOn;

                if ((expiresOn - oAuth2AccessTokenResponse.NotBefore) > AccessTokenLifetimeTolerance)
                {
                    // Make the access token get renewed a bit earlier than the time when it expires
                    // so that the calls to SharePoint with it will have enough time to complete successfully.
                    expiresOn -= AccessTokenLifetimeTolerance;
                }

                accessToken = Tuple.Create(oAuth2AccessTokenResponse.AccessToken, expiresOn);
            }
            catch (WebException)
            {
            }
        }