2015-05-14 46 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实例上时,您只需要从存储它的位置加载它。