通过Dropbox Api V2上传文件

时间:2017-09-29 17:39:50

标签: javascript node.js dropbox dropbox-api

之前我在我的网络应用中使用 Dropbox API V1 来上传我的Dropbox帐户的文件。请注意,该应用仅使用一个Dropbox帐户(我的)来上传文件。

以前:

  1. 我在Dropbox开发者控制台上创建了一个应用
  2. 从开发者控制台生成我的令牌
  3. 将该令牌硬编码到我的服务器中,将所有文件上传到Dropbox中的特定文件夹。
  4. 之前完美无缺,但由于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);
    
                        }
                    });
    
                }
            });
         }
    

    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)
    

    问题:

    我尝试过的东西不起作用。我似乎无法从我的应用程序中将文件上传到我的Dropbox帐户。 我尝试从Dropbox App控制台重新生成我的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
    

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我的建议也是使用一个抽象认证的SDK。 CloudRail for Node.js在这里非常有用。它非常易于使用,也适用于OneDrive等其他提供商。

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

这也是short article about the {“error”: “v1_retired”} issue