2014-02-08 15 views
0

我正在構建需要將文件上傳到DropBox的Windows c#應用程序。基本上,我擁有我需要的所有應用程序(應用程序密鑰和應用程序密鑰),但我需要將客戶機令牌保存到我的sql數據庫中供將來使用。根據Dropbox我無法保存用戶登錄信息,這是很好的,但找到一個好的lib越來越難。我已經嘗試了許多不同的基於DropBox的庫,但運行以下問題:協助使用DropBox使用Oauth2身份驗證

SharpBox:似乎很容易使用,但需要某種解串器來將客戶端密鑰和客戶端密鑰保存在任何地方。

OAuth2授權人:沒有足夠的文檔,我可以找到,以便我實際執行此操作。

DropNet:這看起來很有前途。它是異步的,看起來不錯,但我再次找不到如何執行auth函數並將變量保存到文件/ DB/Reg /或其他任何示例。

DropBox.API:這是我目前使用的方法,它的工作。問題是它不是異步並且需要.NET 4.5。我對所有的問題都很滿意,但最近發現它對於不同版本的JSON和其他庫非常敏感。

我希望有人可以給我一些幫助,以獲得任何上述OAuth庫實際工作,只是爲了讓3段驗證過程工作。

更新::

確定,所以我要去包括一些我使用目前的代碼,使用dropbox.api:

// Get Oauth Token 
    private static OAuthToken GetAccessToken() 
    { 
     string consumerKey = "mykey"; 
     string consumerSecret = "myseceret"; 
     var oauth = new OAuth(); 
     var requestToken = oauth.GetRequestToken(new Uri(DropboxRestApi.BaseUri), consumerKey, consumerSecret); 
     var authorizeUri = oauth.GetAuthorizeUri(new Uri(DropboxRestApi.AuthorizeBaseUri), requestToken); 
     Process.Start(authorizeUri.AbsoluteUri); 
     MessageBox.Show("Once Registration is completed Click OK", "Confirmation"); 
     return oauth.GetAccessToken(new Uri(DropboxRestApi.BaseUri), consumerKey, consumerSecret, requestToken); 
    } 

     // Complete Oauth function and write to file 
    private void button5_Click(object sender, EventArgs e) 
    { 
     DialogResult result1 = MessageBox.Show("Please register for dropbox before continuing with authentication. The authorization process will take 1 minute to complete. During that time the backup utility window will be unresponsive. Click yes if you are ready to begin the authorization. HAVE YOU REGISTERED FOR DROPBOX YET?", "DO YOU HAVE A DROPBOX ACCOUNT?", MessageBoxButtons.YesNo); 
     if (result1 == DialogResult.Yes) 
     { 
      try 
      { 
       u_w.Enabled = false; 
       var accesstoken = GetAccessToken(); 
       StringBuilder newFile = new StringBuilder(); 
       string temptoken = ""; 
       string tempsecret = ""; 
       string tempprovider = ""; 
       string tempstatus = ""; 
       string[] file = System.IO.File.ReadAllLines(@"C:\cfg\andro_backup.ini"); 
       foreach (string line in file) 
       { 
        if (line.Contains("dbkey:")) 
        { 
         temptoken = line.Replace("dbkey:", "dbkey:" + accesstoken.Token); 
         newFile.Append(temptoken + "\r\n"); 
         continue; 
        } 
        if (line.Contains("dbsecret:")) 
        { 
         tempsecret = line.Replace("dbsecret:", "dbsecret:" + accesstoken.Secret); 
         newFile.Append(tempsecret + "\r\n"); 
         continue; 
        } 
        if (line.Contains("Provider:")) 
        { 
         tempprovider = line.Replace("Provider:", "Provider:DropBox"); 
         newFile.Append(tempprovider + "\r\n"); 
         continue; 
        } 
        if (line.Contains("Status:")) 
        { 
         tempstatus = line.Replace("Status:", "Status:Connected"); 
         newFile.Append(tempstatus + "\r\n"); 
         continue; 
        } 
        newFile.Append(line + "\r\n"); 
       } 
       System.IO.File.WriteAllText(@"C:\cfg\andro_backup.ini", newFile.ToString()); 
       MessageBox.Show("Completed Backup Provider Setup", "Provider Setup Complete"); 
       Configuration.Reload(); 

上述工程的時刻,我可以上傳,下載文件。問題是這不是異步,我想嘗試保持在.NET 4.0中,如果可能的話,這個代碼需要4.5

試圖用dropnet做同樣的事情,我甚至無法讓它工作,甚至使用他在頁面https://github.com/dkarzon/DropNet上給出的例子。 我試圖看看他在那裏的演示,但他們解釋每次都有用戶登錄來執行任何功能,我需要應用程序被授權,以便它可以在需要時做到這一點。就我所使用的drop net代碼而言,我實際上只是複製並粘貼了他在那裏的內容,只是爲了看看我是否能夠連接並且仍然沒有去。

+1

您可以使用DropNet發佈您的代碼嗎?看起來使用已保存的訪問令牌非常簡單,但很難在沒有看到您嘗試的內容的情況下說出錯過的內容。 – smarx

回答

-1

如果您使用類似於示例的DropNet,您只需要從GetAccessToken方法中保存返回對象。這將返回一個UserLogin對象的實例,該對象上有令牌和祕密。或者,如果您使用異步方法,則使用回調參數。

結帳樣品在這裏: https://github.com/dkarzon/DropNet/blob/master/DropNet.Samples/DropNet.Samples.WP7/MainPage.xaml.cs#L69

後您正在使用它,所以我可以給你一個更好的解釋代碼。

+0

實際上有幫助,我再看看文件,並最終拼湊起來。謝謝。 –