Cordova 移动应用和 Dropbox API v2

Cordova mobile app and Dropbox API v2

有人在 Cordova 移动应用程序中使用 Dropbox API v2 吗?甚至是移动应用期? API v1 & Cordova 有一个教程: http://ourcodeworld.com/articles/read/149/how-to-use-dropbox-in-a-cordova-application 但 Dropbox 已经或正在弃用它。

我有一个 Github 项目,它是一个基本的 Cordova 项目(版本 6.5.0)并且包含 Dropbox API v2。我可以让项目进入授权屏幕,但我相信我的问题是重定向 URI。

我正在使用:

但是,我已经将所有内容都放入了 Github 存储库中: https://github.com/ModusPwnens1337/dropboxTest

我认为重定向 URI 是问题所在,您可以在 index.html:

的第 128 行找到它
var authUrl = dbx.getAuthenticationUrl('https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=8nvbrxvlg96tx1k&redirect_uri=helloworld://localhost/callback');

请告诉我是否有人已获得或可以获得重定向回移动应用程序的授权。

提前致谢!

您可以尝试将此 Cordova 插件用于 Oauth2(它也使用 In-App-Browser 插件):https://github.com/krisrak/jquery-cordova-oauth2

好的,我想通了! 我在项目中遇到了三个问题:

  1. 我的重定向 URI 是错误的,应该是以下内容:

    var authUrl = dbx.getAuthenticationUrl('helloworld://localhost/callback');

  2. 我没有正确设置自定义 URL 方案,幸运的是有一个漂亮的小插件可以使用:https://github.com/EddyVerbruggen/Custom-URL-scheme。这就是我 运行 在我的 CLI 中安装的插件:cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=helloworld

  3. 添加插件并阅读说明后,您会发现需要添加一个函数来处理回调访问令牌,如下所示:

function handleOpenURL(url) {
    console.log("handleOpenURL: " + url);
    showPageSection('authed-section');
    // Create an instance of Dropbox with the access token and use it to
    // fetch and render the files in the users root directory.
    var dbx = new Dropbox({ accessToken: getAccessTokenFromUrl2(url) });
    dbx.filesListFolder({path: ''})
        .then(function(response) {
            renderItems(response.entries);
        })
        .catch(function(error) {
            console.error(error);
        });
}

并且您还必须编辑 getAccessTokenFromCustomUrl 以用于新回调:

function getAccessTokenFromUrl2(url) {
    url = url.split('#')[1];
    console.log('getAccessTokenFromUrl2: ' + utils.parseQueryString(url).access_token);
    return utils.parseQueryString(url).access_token;
}

注意,不要忘记重定向 uri 需要在 App Console 的应用程序页面上预先注册:https://www.dropbox.com/developers/apps