我尝试使用node-dbox在Dropbox中列出我的内容。我与OAuth建立了连接并确实通过但是当我尝试执行client.metadata函数时,它显示了path.join错误。这是代码......
app / index.js:
// MODULE REQUIERMENTS
var dbox=require('dbox'),
http= require('http'),
fs=require('fs'),
path= require('path'),
express=require('express'),
bodyParser = require('body-parser'),
multipart=require('connect-multiparty');
//PATH JSON AUTH FILE
var auth_path=path.join(__dirname,'../auth.json'),
config = JSON.parse(fs.readFileSync(auth_path)),
dboxapp= dbox.app(config),
token=false,
client = false ;
//NEW EXPRESS
var app = new express();
// MIDDLEWARE FOR MULTIPART
var multipartMiddleware= multipart();
//SET THE JADE VIEWS
app.set('views', __dirname + '/views');
app.set('view engine','jade');
app.use(bodyParser());
app.use(express.static(path.join(__dirname,'public')));
app.use(function(req,res,next) {
if (client || token) {
next();
} else{
dboxapp.requesttoken(function(status,request_token) {
token=request_token;
var oAuthURL = request_token.authorize_url + '&oauth_callback=http://localhost:3000/access';
res.redirect(oAuthURL);
});
};
});
app.get('/access',function (req,res) {
dboxapp.accesstoken(token,function(status,access_token) {
client= dboxapp.client(access_token);
res.redirect('/album.json');
});
});
app.get('/album',function (req,res) {
console.log(client);
client.metadata("/",{list:true},function (status, reply) {
console.log(reply);
res.render('layout',reply);
});
});
app.get('/album.json',function (req,res) {
client.metadata('/ ',function (status, reply) {
res.json(reply);
});
});
module.exports=app;
视图/ layout.jade
doctype html
html
head
title Cova Nosejs API Dropbox
link(rel="stylesheet",href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css")
body
ul
each file in contents
li= file.path
控制台错误消息:
ropboxAPI> Express server (DropboxAPI) listening on port 3000
[Function]
TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at Object.exports.join (path.js:358:36)
at Object.url (/Documents/workspace/DropboxAPI/node_modules/dbox/lib/helpers.js:127:27)
at Object.metadata (/Documents/workspace/DropboxAPI/node_modules/dbox/lib/dbox.js:200:29)
at Object.module.exports [as handle] (/Documents/workspace/DropboxAPI/app/index.js:59:9)
at next_layer (/Documents/workspace/DropboxAPI/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/Documents/workspace/DropboxAPI/node_modules/express/lib/router/route.js:107:5)
at /Documents/workspace/DropboxAPI/node_modules/express/lib/router/index.js:213:24
at Function.proto.process_params (/Documents/workspace/DropboxAPI/node_modules/express/lib/router/index.js:284:12)
答案 0 :(得分:0)
您可以尝试将参数明确强制转换为字符串...
app.use(express.static(path.join(__dirname.toString(),'public')));
答案 1 :(得分:0)
很抱歉,这不是一个很好的答案,但有时它有助于从小做起并解决问题。这里有一些适合我的代码,使用OS X上的节点版本0.10.24和dbox版本0.6.3(当前来自npm):
var APPKEY = '<REDACTED>',
APPSECRET = '<REDACTED>';
var dbox = require('dbox'),
readline = require('readline');
var app = dbox.app({
app_key: APPKEY,
app_secret: APPSECRET
});
app.requesttoken(function (status, requestToken) {
console.log('Go to https://www.dropbox.com/1/oauth/authorize?oauth_token=' + requestToken.oauth_token);
rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('Press enter once you\'ve authorized: ');
rl.prompt();
rl.on('line', function (line) {
app.accesstoken(requestToken, function (status, accessToken) {
var client = app.client(accessToken);
client.metadata('/', function (status, reply) {
console.log(reply);
});
})
rl.close();
});
});
你可以尝试一下这段代码,看看它是否有效?如果是这样,我们可以尝试找出您的代码有何不同之处。 (如果没有,我们可以尝试弄清楚您的环境有何不同。)
答案 2 :(得分:0)
在检查package.json上的依赖项之后,dbox设置为此"dbox": "*"
并且它没有采用最新版本(即0.6.3)。
此案例的修复是指定版本"dbox":"0.6.3"