file upload通过Dropbox的Api V2

以前,我在我的networking应用程序中使用Dropbox API V1上传我的保pipe箱帐户的文件。 请注意,该应用程序只使用一个Dropbox帐户(我的)上传文件。

所以之前:

  1. 我在Dropbox开发者控制台上创build了一个应用程序
  2. 从开发者控制台生成我的令牌
  3. 将该令牌硬编码到我的服务器上,将所有file upload到我的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); } }); } }); } 

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) 

问题:

我所尝试的不起作用。 我似乎无法从我的应用程序中上传文件到我的保pipe箱帐户。 我试图从Dropbox应用程序控制台重新生成我的TOKEN,但没有运气。

谁能告诉我我做错了什么?

更新:

我将代码更新为API的类似结构,但仍无法解决。

  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保pipe箱包,隐藏authentication过程的抽象等等。

检查官方的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); 

我的build议也是使用一个抽象authentication的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 } ); 

这里也是关于{“error”:“v1_retired”}问题的简短文章 。