有没有人成功将Dropbox集成到Windows 8.1 Store应用程序中,以及如何?

时间:2015-04-23 13:22:16

标签: sdk windows-store-apps dropbox dropbox-api dropnet

我有一个针对Windows 8.1的Windows应用商店应用,我需要集成Dropbox。截至目前,仍然没有正式的Dropbox SDK。他们列出了一些选项here。其中一些SDK在多年来没有被触及,这令人不安。

我还需要在不命中服务器的情况下进行身份验证。在iOS上,我通过让我的应用程序在操作系统中注册自定义URI来实现这一点,以便在用户在浏览器中进行身份验证后,使用令牌调用我的应用程序。也许在Windows上需要类似的东西,但我找不到任何人以这种方式设置身份验证的例子。

所以我的问题是:有没有人将Dropbox集成到Windows应用商店应用中,而没有单独的服务器进行身份验证,你是怎么做到的?

1 个答案:

答案 0 :(得分:0)

SOLUTION: I decided to pursue using DropNetRT. I could not find a complete sample of how to authenticate it in a Windows Store app but I finally have a fully working solution for the authentication WITHOUT A SERVER, which was my goal. I'm posting it here in hopes it will save someone else time.

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;
}