Example #1
0
        public async Task <(CloudStorageResult, CloudStorageAccountInfo)> LoginAsync()
        {
            CloudStorageResult      result      = new CloudStorageResult();
            CloudStorageAccountInfo accountInfo = new CloudStorageAccountInfo();

            try
            {
                if (await oauthClient.GetTokenAsync())
                {
                    SaveAccessTokenDelegate?.Invoke(oauthClient.AccessToken);
                    SaveRefreshTokenDelegate?.Invoke(oauthClient.RefreshToken);
                    InitDriveService();
                    (result, accountInfo.userName, accountInfo.userEmail) = await GetUserInfoAsync();

                    if (result.Status == Status.Success)
                    {
                        (result, accountInfo.usedSpace, accountInfo.totalSpace) = await GetRootInfoAsync();
                    }
                }
                else
                {
                    result.Message = oauthClient.LastError;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return(result, accountInfo);
        }
Example #2
0
        public async Task <CloudStorageResult> InitAsync()
        {
            CloudStorageResult result = new CloudStorageResult();

            try
            {
                // 取得上次登入資訊
                string lastRefreshToken = LoadRefreshTokenDelegate?.Invoke();

                // 初始化
                result.Status = Status.NeedAuthenticate;
                oauthClient   = new OneDriveOauthClient(ApiKey, ApiSecret);
                if (!string.IsNullOrEmpty(lastRefreshToken))
                {
                    bool needLogin = !await oauthClient.RefreshTokenAsync(lastRefreshToken);

                    if (needLogin)
                    {
                        lastRefreshToken = null;
                    }
                    else
                    {
                        result.Status = Status.Success;
                        // 儲存新的access token/refresh token
                        SaveAccessTokenDelegate?.Invoke(oauthClient.AccessToken);
                        InitDriveService();
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }
            return(result);
        }
Example #3
0
        public async Task <CloudStorageResult> InitAsync()
        {
            CloudStorageResult result = new CloudStorageResult();
            bool IsNeedLogin          = true;

            try
            {
                // 取得上次登入資訊
                LastAccessToken  = LoadAccessTokenDelegate?.Invoke();
                LastRefreshToken = LoadRefreshTokenDelegate?.Invoke();

                // 初始化
                oAuthWrapper = new DropBoxOauthClient(ApiKey, RedirectUrl);
                if (!string.IsNullOrEmpty(LastRefreshToken))  // offline 類型的 token
                {
                    IsNeedLogin = !await oAuthWrapper.RefreshTokenAsync(LastRefreshToken);

                    if (IsNeedLogin)
                    {
                        result.Status   = Status.NeedAuthenticate;
                        LastAccessToken = LastRefreshToken = null;
                    }
                    else
                    {
                        result.Status = Status.Success;
                        // 儲存新的access token/refresh token
                        SaveAccessTokenDelegate?.Invoke(oAuthWrapper.AccessToken);
                        InitDriveService();
                    }
                }
                else if (!string.IsNullOrEmpty(LastAccessToken))  // legacy 類型的 token,不需要refresh
                {
                    result.Status = Status.Success;
                    InitDriveService();
                }
                else
                {
                    // each token is empty, need login
                    result.Status  = Status.NeedAuthenticate;
                    result.Message = "Empty access and refresh token.";
                }
            }
            catch (Exception ex)
            {
                result.Status  = Status.UnknownError;
                result.Message = ex.Message;
            }
            return(result);
        }
Example #4
0
        public CloudStorageResult AuthenticateFromUri(string state, string uri)
        {
            CloudStorageResult result = new CloudStorageResult();

            try
            {
                bool success = oAuthWrapper.ProcessUri(state, uri);
                if (success)
                {
                    result.Status    = Status.Success;
                    LastAccessToken  = oAuthWrapper.AccessToken;
                    LastRefreshToken = oAuthWrapper.RefreshToken;
                    SaveAccessTokenDelegate?.Invoke(LastAccessToken);
                    SaveRefreshTokenDelegate?.Invoke(LastRefreshToken);
                    InitDriveService();
                }
                result.Status = Status.NeedAuthenticate;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return(result);
        }