示例#1
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);

            var authProvider = new MsaAuthenticationProvider(Secrets.CLIENT_ID, Secrets.CLIENT_SECRET, LIVE_LOGIN_DESKTOP_URI, scopes, default(CredentialCache));
            var oauthHelper  = new OAuthHelper();

            if (!string.IsNullOrEmpty(refreshToken))
            {
                authProvider.CurrentAccountSession = await oauthHelper.RedeemRefreshTokenAsync(refreshToken, Secrets.CLIENT_ID, Secrets.CLIENT_SECRET, LIVE_LOGIN_DESKTOP_URI, scopes);
            }
            else
            {
                if (string.IsNullOrEmpty(code))
                {
                    code = await oauthHelper.GetAuthorizationCodeAsync(Secrets.CLIENT_ID, LIVE_LOGIN_DESKTOP_URI, scopes, new WebAuthenticationUi(account));
                }

                authProvider.CurrentAccountSession = await oauthHelper.RedeemAuthorizationCodeAsync(code, Secrets.CLIENT_ID, Secrets.CLIENT_SECRET, LIVE_LOGIN_DESKTOP_URI, scopes);

                if (string.IsNullOrEmpty(authProvider.CurrentAccountSession?.AccessToken))
                {
                    throw new AuthenticationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.RetrieveAuthenticationCodeFromUri, LIVE_LOGIN_AUTHORIZE_URI));
                }
            }

            var client = new OneDriveClient(authProvider);

            SaveRefreshToken(account, authProvider.CurrentAccountSession.RefreshToken, settingsPassPhrase);

            return(client);
        }