File Uploads via Dropbox Api V2

前提是你 提交于 2019-12-02 00:07:17

问题


Previously I was using the Dropbox API V1 within my web app to upload files my dropbox account. Please note that the app uses only one dropbox account (mine) to upload files.

So Previously:

  1. I created an app on the dropbox developers console
  2. Generated my token from the developers console
  3. Hard coded that token into my server to upload all file to a specific folder within my Dropbox.

This worked perfectly before but as the dropbox API v1 has been deprecated it does not work anymore.

Dropbox V1 Code:

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 Code:

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)

Issue:

What I have tried does not work. I can't seem to upload a file to my dropbox account from within my app. I have tried re-generating my TOKEN from the Dropbox App console but no luck.

Can anyone tell me what am I doing wrong?

Update:

I updated my code to similar structure for v2 of the API but still unable to resolve it.

 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

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/46494247/file-uploads-via-dropbox-api-v2

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