Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }