public async Task GetSilentlyAuthenticatedMicrosoftAccountClient_WithSecret()
        {
            var appId        = "appId";
            var clientSecret = "secret";
            var refreshToken = "refresh";
            var returnUrl    = "returnUrl";
            var scopes       = new string[] { "scope" };

            this.authenticationProvider.SetupSet(provider => provider.CurrentAccountSession = It.IsAny <AccountSession>()).Verifiable();

            this.SetupServiceInfoProviderForMicrosoftAccount(appId, clientSecret, returnUrl, scopes);

            var client = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(
                appId,
                returnUrl,
                scopes,
                clientSecret,
                refreshToken,
                this.serviceInfoProvider.Object,
                this.credentialCache.Object,
                this.httpProvider.Object);

            this.authenticationProvider.VerifySet(
                provider => provider.CurrentAccountSession = It.Is <AccountSession>(session => refreshToken.Equals(session.RefreshToken)),
                Times.Once);

            this.authenticationProvider.Verify(provider => provider.AuthenticateAsync(), Times.Once);
        }
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            if (ApplicationData.Current.LocalSettings.Values.ContainsKey("RefreshToken"))
            {
                string          refreshToken = ApplicationData.Current.LocalSettings.Values["RefreshToken"].ToString();
                string          returnUrl    = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();
                IOneDriveClient client       = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(ClientId, returnUrl, Scopes,
                                                                                                                   refreshToken);

                bool isFolderExisting = true;
                try
                {
                    await client.Drive.Root.ItemWithPath("OneDrive Sample").Request().GetAsync();
                }
                catch (OneDriveException exc)
                {
                    isFolderExisting = false;
                }

                if (isFolderExisting)
                {
                    StorageFile file = await Package.Current.InstalledLocation.GetFileAsync("BackgroundWallpaper.jpg");

                    using (Stream stream = await file.OpenStreamForReadAsync())
                    {
                        await client.Drive.Root.ItemWithPath("OneDrive Sample/BackgroundWallpaper.png").Content.Request().PutAsync <Item>(stream);
                    }
                }
            }

            deferral.Complete();
        }
Exemple #3
0
 public static async Task <IOneDriveClient> GetOneDriveClientForAccountAsync(Account account)
 {
     try
     {
         return(await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(AppClientID, RedirectUri, Scopes, AppClientSecret, account.RefreshToken));
     }
     catch (OneDriveException ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.ToString());
     }
     return(null);
 }
Exemple #4
0
        public static async Task <IOneDriveClient> LoginAsync(string account, string code, string settingsPassPhrase)
        {
            if (string.IsNullOrEmpty(account))
            {
                throw new ArgumentNullException(nameof(account));
            }

            var refreshToken = LoadRefreshToken(account, settingsPassPhrase);

            IOneDriveClient client;

            if (!string.IsNullOrEmpty(refreshToken))
            {
                client = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET, refreshToken);
            }
            else
            {
                if (string.IsNullOrEmpty(code))
                {
                    client = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET, new WebAuthenticationUi(account));
                }
                else
                {
                    client = OneDriveClient.GetMicrosoftAccountClient(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, Secrets.CLIENT_SECRET);
                    client.AuthenticationProvider.CurrentAccountSession = new AccountSession()
                    {
                        AccessToken = code
                    };
                }

                await client.AuthenticateAsync();

                if (!client.IsAuthenticated)
                {
                    throw new AuthenticationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.RetrieveAuthenticationCodeFromUri, LIVE_LOGIN_AUTHORIZE_URI));
                }
            }

            SaveRefreshToken(account, client.AuthenticationProvider.CurrentAccountSession.RefreshToken, settingsPassPhrase);

            return(client);
        }
Exemple #5
0
 public async Task <IOneDriveClient> InitClient()
 {
     // SimpleOneDriveAuthProvider auth = new SimpleOneDriveAuthProvider() { AccessToken = authstate.AccessToken };
     return(await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(AppKey, string.Empty, Scopes, AppSecret, AuthState.RefreshToken));
 }