Search code examples
javascriptnode.jsexpressdropboxdropbox-api

How do I use the Dropbox npm package to upload data from an express server


I have a file on a server which is updated pretty regularly by calls to an API. When it is, I would like to send a copy of that file to a location on Dropbox. I've been following the documentation for the Dropbox npm package, but I get an error for a missing or malformed Auth header when I actually try running the code. My implementation is as follows:


const Dropbox = require("dropbox").Dropbox;
const dbx = new Dropbox({ accessToken: DROPBOX_TOKEN });

async function sendToDropbox(uploadPath, fileContents){
    try {
        dbx.filesUpload({ path: uploadPath, contents: fileContents })
            .then(response => {
                console.log(response);
            })
            .catch(error => {
                console.error(error);
            });
    } catch (e) {
        console.error("Could not upload file:\n"+e);
    }
}

I was expecting the above code - authenticated with my access token - to upload the file given to the function to the destination I specified. Instead, I get the error error: 'Error in call to API function "files/upload": Must provide HTTP header "Authorization" or URL parameter "authorization".'


Solution

  • Based on the error you're getting, you didn't supply an access token. Make sure your DROPBOX_TOKEN actually contains your access token string. You may want to add some logging or use a debugger to check that.