public async Task <bool> SignIn()
        {
            this.tcs = new TaskCompletionSource <bool>();
            try
            {
                // If it haven't registerd live client, register
                LiveConnectClient liveClient = await this.GetLiveConnectClientAsync();

                this.LiveClient = liveClient;

                // Get id and name.
                LiveOperationResult operationResult = await this.LiveClient.GetAsync("me");

                string accountId       = (string)operationResult.Result["id"];
                string accountUserName = (string)operationResult.Result["name"];

                // Register account
                await TaskHelper.WaitTask(App.AccountManager.GetPtcId());

                StorageAccount storageAccount = await App.AccountManager.GetStorageAccountAsync(accountId);

                if (storageAccount == null)
                {
                    storageAccount             = new StorageAccount();
                    storageAccount.Id          = accountId;
                    storageAccount.StorageName = this.GetStorageName();
                    storageAccount.UserName    = accountUserName;
                    storageAccount.UsedSize    = 0.0;
                    await App.AccountManager.CreateStorageAccountAsync(storageAccount);
                }
                this.CurrentAccount = storageAccount;

                // Save sign in setting.
                App.ApplicationSettings[ONE_DRIVE_SIGN_IN_KEY] = true;
                App.ApplicationSettings.Save();
                TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                tcs.SetResult(true);
            }
            catch
            {
                tcs.SetResult(false);
            }

            return(tcs.Task.Result);
        }
Beispiel #2
0
        public async Task <bool> SignIn()
        {
            this.tcs = new TaskCompletionSource <bool>();
            // Add application settings before work for good UX
            try
            {
                //Uri clientSecretsUri = new Uri("/Utilities/google_secret.json",UriKind.Relative);
                //new ClientSecrets
                //    {
                //        ClientId = GOOGLE_DRIVE_CLIENT_ID,
                //        ClientSecret = GOOGLE_DRIVE_CLIENT_SECRET
                //    }
                this.Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    new Uri("ms-appx:///Assets/client_secret.json"),
                    new[] { DriveService.Scope.Drive },
                    this._GetUserSession(),
                    CancellationToken.None
                    );

                this.Service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = this.Credential,
                    ApplicationName       = "athere",
                });
                AboutResource aboutResource = this.Service.About;
                About         about         = await aboutResource.Get().ExecuteAsync();

                this.User = about.User;

                string name = this.User.DisplayName;
                string id   = about.PermissionId;

                // Register account
                StorageAccount account = await App.AccountManager.GetStorageAccountAsync(id);

                if (account == null)
                {
                    account = new StorageAccount(id, StorageAccount.StorageAccountType.GOOGLE_DRIVE, name, 0.0);
                    await App.AccountManager.CreateStorageAccountAsync(account);
                }
                this.CurrentAccount = account;

                // Save sign in setting.
                App.ApplicationSettings[GOOGLE_DRIVE_SIGN_IN_KEY] = true;
                App.ApplicationSettings.Save();
                TaskHelper.AddTask(TaskHelper.STORAGE_EXPLORER_SYNC + this.GetStorageName(), StorageExplorer.Synchronize(this.GetStorageName()));
                tcs.SetResult(true);
            }
            catch (Google.GoogleApiException e)
            {
                Debug.WriteLine(e.ToString());
                System.Diagnostics.Debugger.Break();
                tcs.SetResult(false);
            }
            catch (System.Threading.Tasks.TaskCanceledException)
            {
                tcs.SetResult(false);
                System.Diagnostics.Debugger.Break();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                tcs.SetResult(false);
                System.Diagnostics.Debugger.Break();
            }

            return(tcs.Task.Result);
        }