Sky*_*ker 5 javascript dropbox node.js dropbox-api
以前,我在网络应用程序中使用Dropbox API V1来上传我的 Dropbox 帐户的文件。请注意,该应用程序仅使用一个 Dropbox 帐户(我的)来上传文件。
所以以前:
这之前工作得很好,但由于 dropbox API v1 已被弃用,它不再工作了。
Dropbox V1 代码:
function fileupload(content) {
request.put('https://api-content.dropbox.com/1/files_put/auto/my_reports/report.pdf', {
headers: {
Authorization: 'TOKEN HERE',
'Content-Type': 'application/pdf'
},
body: content
}, function optionalCallback(err, httpResponse, bodymsg) {
if (err) {
console.log(err);
}
else {
console.log("File uploaded to dropbox successfully!");
fs.unlink(temp_dir + 'report.pdf', function(err) {
if (err)
throw err;
else {
console.log("file deleted from server!");
}
})
request.post('https://api.dropboxapi.com/1/shares/auto/MY_reports/report.pdf' + '?short_url=false', {
headers: {
Authorization: 'TOKEN HERE'
}
}, function optionalCallback(err, httpResponse, bodymsg) {
if (err) {
console.log(err);
}
else {
console.log('Shared link 2 ' + JSON.parse(httpResponse.body).url);
}
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
Dropbox V2 代码:
function fileupload(content) {
request.post('https://content.dropboxapi.com/2/files/upload/my_reports', {
headers: {
Authorization: 'TOKEN HERE',
'Content-Type': 'application/pdf'
},
body: content
} ......... (rest of the code is similar to above)
Run Code Online (Sandbox Code Playgroud)
问题:
我尝试过的方法不起作用。我似乎无法从我的应用程序内将文件上传到我的 Dropbox 帐户。我尝试从 Dropbox 应用控制台重新生成我的 TOKEN,但没有成功。
谁能告诉我我做错了什么?
更新:
我将代码更新为 API v2 的类似结构,但仍然无法解决它。
request.post('https://content.dropboxapi.com/2/files/upload/', {
headers: {
Authorization: 'Bearer TOKEN',
'Dropbox-API-Arg': {"path": "/Homework","mode": "add","autorename": true,"mute": false},
'Content-Type': 'application/pdf'
//'Content-Type': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
},
body: content
} .... similar code
Run Code Online (Sandbox Code Playgroud)
小智 1
我鼓励您使用现有的 nodejs dropbox 包,它隐藏了身份验证过程的抽象等。
检查官方dropbox-sdk-js或尝试我的小包dropbox-v2-api。快速示例:
const dropboxV2Api = require('dropbox-v2-api');
//create session
const dropbox = dropboxV2Api.authenticate({
token: 'TOKEN HERE'
});
//create upload stream
const uploadStream = dropbox({
resource: 'files/upload',
parameters: {
path: '/dropbox/path/to/file.txt'
}
}, (err, result) => {
// upload completed
});
//use nodejs stream
fs.createReadStream('path/to/file.txt').pipe(uploadStream);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19141 次 |
| 最近记录: |