예제 #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);
        }
예제 #2
0
        /// <summary>
        /// Create a new instance of the OneDriveClient for the signed in user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        internal static async Task <OneDriveClient> GetOneDriveClientAsync(OneDriveUser user)
        {
            if (string.IsNullOrEmpty(user.OneDriveBaseUrl))
            {
                // Resolve the API URL for this user
                user.OneDriveBaseUrl = await LookupOneDriveUrl(user);
            }

            var client = new OneDriveClient(new AppConfig(), null, null, new OneDriveAccountServiceProvider(user), user.ClientType);
            await client.AuthenticateAsync();

            return(client);
        }
        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);
            }
        }