2015-05-14 21 views
0

我試圖存儲用戶令牌,一旦我的應用程序被授權與Dropbox,以便當我打開一個不同的表單(這將具有拖放功能),用戶不會必須再次授權應用程序,並能夠執行Dropbox的上傳功能。使用DropNet從Dropbox存儲用戶令牌

我用於授權的Dropbox類DropNet是:

我宣佈兩個屬性; public string UserToken { get; set; }public string UserSecret { get; set; }

string appKey = "APP_KEY"; 
    string appSecret = "APP_SECRET"; 

    public bool LinkDrpbox() 
    { 
     bool dropboxLink = false; 

     Authenticate(
      url => 
      { 
       var proc = Process.Start("iexplore.exe", url); 
       proc.WaitForExit(); 
       Authenticated(
        () => 
        { 
         dropboxLink = true; 
        }, 
        exc => ShowException(exc)); 

      }, 
      ex => dropboxLink = false); 

     if (dropboxLink) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    private DropNetClient _Client; 
    public DropNetClient Client 
    { 
     get 
     { 
      if (_Client == null) 
      { 
       _Client = new DropNetClient(appKey, appSecret); 

       if (IsAuthenticated) 
       { 
        _Client.UserLogin = new UserLogin 
        { 
         Token = UserToken, 
         Secret = UserSecret 
        }; 
       } 

       _Client.UseSandbox = true; 
      } 
      return _Client; 
     } 
    } 

    public bool IsAuthenticated 
    { 
     get 
     { 
      return UserToken != null && 
       UserSecret != null; 
     } 
    } 

    public void Authenticate(Action<string> success, Action<Exception> failure) 
    { 
     Client.GetTokenAsync(userLogin => 
     { 
      var url = Client.BuildAuthorizeUrl(userLogin); 
      if (success != null) success(url); 
     }, error => 
     { 
      if (failure != null) failure(error); 
     }); 
    } 

    public void Authenticated(Action success, Action<Exception> failure) 
    { 
     Client.GetAccessTokenAsync((accessToken) => 
     { 
      UserToken = accessToken.Token; 
      UserSecret = accessToken.Secret; 

      if (success != null) success(); 
     }, 
     (error) => 
     { 
      if (failure != null) failure(error); 
     }); 
    } 

    private void ShowException(Exception ex) 
    { 
     string error = ex.ToString(); 
    } 
} 

我可以授權我的應用程序,但我不能確定如何保存訪問令牌。我假設在app.config文件但不確定。

任何幫助,將不勝感激!

+0

可能需要更多信息,即。你在哪個平臺上?網?的WinForms? WPF? – dkarzon

+0

啊WinForms .... –

+0

你想存儲多久的令牌?會議?跨應用關閉/打開?用戶資料? – dkarzon

回答

1

這是一個.net問題,而不是一個DropNet特定的問題。

看看這個答案https://stackoverflow.com/a/3032538/75946我不同意使用註冊表,但其他2是好的。

當您啓動應用程序並將其設置在您的DropNetClient實例上時,您只需要從存儲它的位置加載它。