简体   繁体   English

是否有人成功将Dropbox集成到Windows 8.1 Store应用程序中?

[英]Has anyone successfully integrated Dropbox into a Windows 8.1 Store app, and how?

I have a Windows Store app targeted for Windows 8.1 and I need to integrate Dropbox. 我有一个针对Windows 8.1的Windows应用商店应用,我需要集成Dropbox。 As of now there is still no official Dropbox SDK. 截至目前,还没有官方的Dropbox SDK。 They list some options here . 他们在这里列出了一些选项。 Some of those SDK's have not been touched in years which is disconcerting. 这些SDK中的一些在多年以来没有被触及过,这令人感到不安。

I also need to authenticate without hitting a server. 我还需要在不访问服务器的情况下进行身份验证。 On iOS I achieve that by having my app register a custom URI with the operating system so that my app gets invoked with a token after the user authenticates in a browser. 在iOS上,我可以通过让我的应用在操作系统中注册自定义URI来实现,以便在用户在浏览器中进行身份验证后使用令牌来调用我的应用。 Maybe something similar is required on Windows but i can't find any examples where someone set up authentication that way. 也许在Windows上需要类似的东西,但是我找不到有人以这种方式设置身份验证的任何示例。

So my question is: has anyone integrated Dropbox into a Windows Store app without a separate server to authenticate, and how did you do it? 所以我的问题是:是否有人在没有单独的服务器进行身份验证的情况下将Dropbox集成到Windows Store应用程序中,您是如何做到的?

SOLUTION: I decided to pursue using DropNetRT. 解决方案:我决定继续使用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. 我无法在Windows Store应用程序中找到有关如何进行身份验证的完整示例,但是我终于有了一个无需服务器即可进行身份验证的完全可行的解决方案,这是我的目标。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM