Exemple #1
0
        /// <summary>
        /// Gets the universal client (Windows 10) and attempts to authenticate with OneDrive.
        /// </summary>
        /// <returns>True if the authentication was successful, otherwise false.</returns>
        public async Task <bool> InitializeAsync()
        {
            var scopes   = new[] { "wl.offline_access", "wl.signin", "office.onenote_update", "onedrive.readwrite" };
            var redirect = "urn:ietf:wg:oauth:2.0:oob";

            OneDriveClient = OneDriveClientExtensions.GetUniversalClient(scopes, redirect) as OneDriveClient;

            if (OneDriveClient == null)
            {
                return(false);
            }

            try
            {
                await OneDriveClient.AuthenticateAsync();

                Debug.WriteLine("Successfully authenticated with OneDrive");
            }
            catch (OneDriveException ex)
            {
                Debug.WriteLine("error authenticating" + ex);
                OneDriveClient.Dispose();
                return(false);
            }

            return(OneDriveClient.IsAuthenticated);
        }
        private async void InitializeClient(ClientType clientType, RoutedEventArgs e)
        {
            if (((App)Application.Current).OneDriveClient == null)
            {
                OneDriveClient client = null;

                try
                {
                    if (clientType == ClientType.Consumer)
                    {
                        client = OneDriveClientExtensions.GetUniversalClient(this.scopes) as OneDriveClient;

                        await client.AuthenticateAsync();
                    }
                    else
                    {
                        client = await BusinessClientExtensions.GetAuthenticatedClientAsync(
                            new AppConfig
                        {
                            ActiveDirectoryAppId     = oneDriveForBusinessClientId,
                            ActiveDirectoryReturnUrl = oneDriveForBusinessReturnUrl,
                        }) as OneDriveClient;
                    }

                    ((App)Application.Current).OneDriveClient = client;
                    ((App)Application.Current).NavigationStack.Add(new ItemModel(new Item()));
                    Frame.Navigate(typeof(MainPage), e);
                }
                catch (OneDriveException exception)
                {
                    // Swallow the auth exception but write message for debugging.
                    Debug.WriteLine(exception.Error.Message);

                    if (client != null)
                    {
                        client.Dispose();
                    }
                }
            }
            else
            {
                Frame.Navigate(typeof(MainPage), e);
            }
        }