通过Dropbox Api V2上传文件 [英] File Uploads via Dropbox Api V2

查看:106
本文介绍了通过Dropbox Api V2上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

以前:


  1. 我在Dropbox开发者控制台上创建了一个应用程序

  2. 从开发者控制台生成我的令牌

  3. 将该令牌硬编码到我的服务器中以将所有文件上传到Dropbox中的特定文件夹。

这之前完美无缺,但是Dropbox API v1已弃用,不再有效。

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

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 App控制台重新生成我的TOKEN,但没有运气。

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?

更新:

我将我的代码更新为v2的类似结构API仍然无法解决它。

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


推荐答案

我鼓励你使用现有的nodejs dropbox包,这些包隐藏了抽象认证过程等等。

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

检查官方 dropbox-sdk-js 或试用我的小包 dro PBOX-V2-API 。简单示例:

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

这篇关于通过Dropbox Api V2上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆