コード例 #1
0
        public override async Task Run()
        {
            try
            {
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.CloudStorageCredentials, out SerializeableCloudStorageCredentials credentials))
                {
                    throw new ArgumentNullException(nameof(credentials));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthState, out string oauthState))
                {
                    throw new ArgumentNullException(nameof(oauthState));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthCodeVerifier, out string oauthCodeVerifier))
                {
                    throw new ArgumentNullException(nameof(oauthState));
                }
                if (!StoryBoard.TryLoadFromSession(SynchronizationStorySessionKey.OauthRedirectUrl, out string redirectUrl))
                {
                    throw new ArgumentNullException(nameof(redirectUrl));
                }

                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthState);
                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthCodeVerifier);
                StoryBoard.RemoveFromSession(SynchronizationStorySessionKey.OauthRedirectUrl);

                ICloudStorageClient cloudStorageClient = _cloudStorageClientFactory.GetOrCreate(credentials.CloudStorageId);
                if (cloudStorageClient is IOAuth2CloudStorageClient oauthStorageClient)
                {
                    CloudStorageToken token = await oauthStorageClient.FetchTokenAsync(redirectUrl, oauthState, oauthCodeVerifier);

                    if (token != null)
                    {
                        // User has granted access.
                        credentials.Token = token;
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.ExistsCloudRepository);
                    }
                    else
                    {
                        // User has rejected access.
                        _feedbackService.ShowToast(_languageService.LoadText("sync_reject"));
                        await StoryBoard.ContinueWith(SynchronizationStoryStepId.StopAndShowRepository);
                    }
                }
            }
            catch (Exception ex)
            {
                // Keep the current page open and show the error message
                ShowExceptionMessage(ex, _feedbackService, _languageService);
            }
        }