有人让 Dropbox API v2 在 Cordova 移动应用程序中工作吗?甚至是移动应用期? 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。
我正在使用:
- Cordova 6.5.0
- Android 测试
- Dropbox API v2:https://github.com/dropbox/dropbox-sdk-js
- 授权示例:https://github.com/dropbox/dropbox-sdk-js/blob/master/examples/auth/index.html
不过,我已经把所有东西都放到了 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');
请告诉我是否有人已获得或可以获得重定向回移动应用程序的授权。
提前致谢!
最佳答案
好吧,我已经想通了! 我在项目中遇到了三个问题:
我的重定向 URI 是错误的,应该是以下内容:
var authUrl = dbx.getAuthenticationUrl('helloworld://localhost/callback');
我没有正确设置自定义 URL 方案,幸运的是有一个漂亮的小插件可以使用:https://github.com/EddyVerbruggen/Custom-URL-scheme .这是我在 CLI 中运行的安装插件的命令:cordova plugin add cordova-plugin-customurlscheme --variable URL_SCHEME=helloworld
添加插件并阅读说明后,您会发现需要添加一个函数来处理回调访问 token ,如下所示:
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
关于android - Cordova 移动应用程序和 Dropbox API v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42400103/