File Uploads via Dropbox Api V2

让人想犯罪 __ 提交于 2019-12-01 20:50:05

I encourage you to use existing nodejs dropbox packages, which hides abstraction of an authentication process, etc. under the hood.

Check official dropbox-sdk-js or try my tiny package dropbox-v2-api. Quick example:

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);

My recommendation is also to use a SDK which abstracts over authentication. CloudRail for Node.js could be very useful here. It's quite easy to use and works for other providers like OneDrive as well.

const cloudrail = require("cloudrail-si");

const service = new cloudrail.services.Dropbox(
    cloudrail.RedirectReceivers.getLocalAuthenticator(8082),
    "[Dropbox Client Identifier]",
    "[Dropbox Client Secret]",
    "http://localhost:8082/auth",
    "someState"
);

service.upload(
    "/myFolder/myFile.png",
    readableStream,
    1024,
    true,
    (error) => {
        // Check for potential error
    }
);

Here is also a short article about the {“error”: “v1_retired”} issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!