2015-04-23 34 views
0

我有一個針對Windows 8.1的Windows應用商店應用程序,我需要整合Dropbox。截至目前,還沒有官方的Dropbox SDK。他們列出了一些選項here。這些SDK中的一些在多年來一直沒有被觸及,這是令人不安的。有沒有人成功將Dropbox整合到Windows 8.1 Store應用程序中,以及如何操作?

我還需要進行身份驗證而不需要訪問服務器。在iOS上,我通過讓應用程序向操作系統註冊自定義URI來實現這一目標,以便在用戶在瀏覽器中進行身份驗證後,使用令牌調用我的應用程序。也許在Windows上需要類似的東西,但我找不到任何有人以這種方式設置認證的例子。

所以我的問題是:有沒有人將Dropbox整合到Windows Store應用程序中,而無需單獨的服務器進行身份驗證,以及您是如何做到的?

+0

你看過https://blogs.dropbox.com/developers/2014/04/dropbox-authorization-in-a-windows-store-app/? – smarx

+0

哇,不,謝謝@smarx!不知何故,我錯過了。這似乎使認證部分變得容易。然後,我仍然需要一種方法來實際檢索文件夾元數據,創建文件夾,上傳/下載文件以及刪除文件。我目前正在調查DropNet(http://dropnet.github.io/dropnetrt.html),因爲我想要比REST調用更高級別的API,我不相信有官方的Dropbox SDK。 –

+0

是的,DropNet可能是一個不錯的選擇。 – smarx

回答

0

解決方案:我決定追求使用DropNetRT。我找不到如何在Windows應用商店應用中對其進行身份驗證的完整示例,但終於有了一個完全可行的解決方案,用於身份驗證WITHOUT A SERVER,這是我的目標。我在這裏發佈它希望它可以節省別人的時間。

private const string VAULT_KEY_DROPBOX_CREDS = "com.mycompany.dropbox.creds"; 
private const string AppKey = "myappkey"; 
private const string AppSecret = "myappsecret"; 

static private string UserToken = null; 
static private string UserSecret = null; 
static public DropNetClient Client = null; 
static private bool IsLoggedIn = false; 

public static async Task<bool> Authorize() 
{ 
    if (!IsLoggedIn) 
    { 
     // first try to retrieve credentials from the PasswordVault 
     PasswordVault vault = new PasswordVault(); 
     PasswordCredential savedCreds = null; 
     try 
     { 
      savedCreds = vault.FindAllByResource(VAULT_KEY_DROPBOX_CREDS).FirstOrDefault(); 
     } 
     catch (Exception) 
     { 
      // this happens when no credentials have been stored 
     } 

     if (savedCreds != null) 
     { 
      savedCreds.RetrievePassword(); 
      UserToken = savedCreds.UserName; 
      UserSecret = savedCreds.Password; 

      // since credentials were found in PasswordVault, they can be used to create an authorized client immediately 
      Client = new DropNetClient(AppKey, AppSecret, UserToken, UserSecret); 
      IsLoggedIn = true; 
     } 
     else 
     { 
      // no credentials were found in PasswordVault, so we need for the user to authenticate 
      // start with a shell of a DropNetClient 
      Client = new DropNetClient(AppKey, AppSecret); 

      // build a request uri so the user can authenticate, and configure it to return control to the app when complete 
      UserLogin requestToken = await Client.GetRequestToken(); 
      Uri appCallbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri(); 
      string requestUrlString = Client.BuildAuthorizeUrl(requestToken, appCallbackUri.ToString()); 
      Uri requestUri = new Uri(requestUrlString); 

      // this call presents the standard Windows "Connecting to a service" screen to let the user authenticate 
      // then returns control once the user either authenticates or cancels (uses the back arrow button) 
      WebAuthenticationResult war = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, requestUri, null); 

      string authCode = null; 
      string uid = null; 
      if (war.ResponseStatus == WebAuthenticationStatus.Success) 
      { 
       // the user successfully authorized the app to use Dropbox, but we are still not done 
       // parse the oauth_token and uid out of the response (although the uid is not needed for anything) 
       string response = war.ResponseData; 
       WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(new Uri(response).Query); 
       authCode = decoder.GetFirstValueByName("oauth_token"); 
       uid = decoder.GetFirstValueByName("uid"); 

       if (authCode != null) 
       { 
        // update the DropNetClient with the authCode and secret 
        // the secret does not change during authentication so just use the one from the requestToken 
        Client.SetUserToken(authCode, requestToken.Secret); 

        // this is the last step: getting a final access token 
        UserLogin finalAccessToken = await Client.GetAccessToken(); 

        // now that we have full credentials, save them in the PasswordVault 
        // so that the user will not need to authenticate the next time 
        if (finalAccessToken != null) 
        { 
         UserToken = finalAccessToken.Token; 
         UserSecret = finalAccessToken.Secret; 
         vault.Add(new PasswordCredential(VAULT_KEY_DROPBOX_CREDS, UserToken, UserSecret)); 
        } 

        IsLoggedIn = true; 
       } 
      } 
     } 
    } 

    return IsLoggedIn; 
} 

public static void Deauthorize() 
{ 
    // to deauthorize dropbox, we just have to find any saved credentials and delete them 
    PasswordVault vault = new PasswordVault(); 
    try 
    { 
     PasswordCredential savedCreds = vault.FindAllByResource(VAULT_KEY_DROPBOX_CREDS).FirstOrDefault(); 
     vault.Remove(savedCreds); 
    } 
    catch (Exception) 
    { 
     // this happens when no credentials have been stored 
    } 

    IsLoggedIn = false; 
} 
相關問題