/// <summary>
        /// Gets a valid authentication token. Also refreshes the access token if it has expired.
        /// </summary>
        /// <remarks>
        /// Used by the API request generators before making calls to the OneNote APIs.
        /// </remarks>
        /// <returns>valid authentication token</returns>
        internal static async Task <AuthenticationResult> GetAuthenticationResult()
        {
            if (String.IsNullOrEmpty(AccessToken))
            {
                try
                {
                    //look to see if we have an authentication context in cache already
                    //we would have gotten this when we authenticated previously
                    var allCachedItems   = AuthContext.TokenCache.ReadItems();
                    var validCachedItems = allCachedItems
                                           .Where(i => i.ExpiresOn > DateTimeOffset.UtcNow.UtcDateTime && IsO365Token(i.IdentityProvider))
                                           .OrderByDescending(e => e.ExpiresOn);
                    var cachedItem = validCachedItems.First();
                    if (cachedItem != null)
                    {
                        //re-bind AuthenticationContext to the authority source of the cached token.
                        //this is needed for the cache to work when asking for a token from that authority.
#if WINDOWS_PHONE_APP
                        AuthContext = AuthenticationContext.CreateAsync(cachedItem.Authority, true).GetResults();
#else
                        AuthContext = new AuthenticationContext(cachedItem.Authority, true);
#endif

                        //try to get the AccessToken silently using the resourceId that was passed in
                        //and the client ID of the application.
                        _authenticationResult = await AuthContext.AcquireTokenSilentAsync(GetResourceHost(ResourceUri), ClientId);

                        RefreshAuthTokenIfNeeded().Wait();
                    }
                }
                catch (Exception)
                {
                    //not in cache; we'll get it with the full oauth flow
                }
            }

            if (string.IsNullOrEmpty(AccessToken))
            {
                try
                {
                    AuthContext.TokenCache.Clear();
#if WINDOWS_PHONE_APP
                    _authenticationResult = await AuthContext.AcquireTokenSilentAsync(GetResourceHost(ResourceUri), ClientId);

                    if (_authenticationResult == null || string.IsNullOrEmpty(_authenticationResult.AccessToken))
                    {
                        AuthContext.AcquireTokenAndContinue(GetResourceHost(ResourceUri), ClientId, new Uri(RedirectUri), null);
                    }
#else
                    _authenticationResult =
                        await AuthContext.AcquireTokenAsync(GetResourceHost(ResourceUri), ClientId, new Uri(RedirectUri));
#endif
                }
                catch (Exception)
                {
                    // Authentication failed
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
                }
            }

            return(_authenticationResult);
        }