7

I have an app that via Dropbox Javascript SDK trying to download the file. I don't have any idea what is wrong. Accessing Dropbox API via fetch calls directly bringing the same error. Dropbox API documentation saying that error 400 is for bad input parameters while it looks like what I'm sending is ok - "Dropbox-API-Arg":"{\"path\":\"/1/price.xlsx\"}"

const Dropbox = require("dropbox").Dropbox;
import axios from "axios";
import fs = require("fs");
import { logger } from "./logger";

export class FileHandler {
    public async handle(path: string, token: string): Promise<void> {
        try {
            const dbx = new Dropbox({ fetch: axios, accessToken: token });
            dbx.filesDownload({ path })
                .then((data) => {
                    fs.writeFile(data.name, data.fileBinary, "binary", (err) => {
                        if (err) { throw err; }
                    });
                })
                .catch((error: any) => {
                    logger.error(error);
                    throw new Error(error);
                });

        } catch (err) {
            logger.error(err);
        }
    }
}
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121
  • [Cross-linking for reference: https://www.dropboxforum.com/t5/Discuss-Developer-API/Error-400-when-trying-to-access-Dropbox-API-via-Dropbox-SDK/m-p/386316#M921 ] – Greg Dec 30 '19 at 15:55
  • Please add the full error also. Error 400 can be due to other reasons as well. Also, try the same with `curl -X POST https://content.dropboxapi.com/2/files/download --header "Authorization: Bearer " --header "Dropbox-API-Arg: {\"path\": \"/1/price.xlsx\"}"` from terminal to see if the error is same – Sunil Chaudhary Dec 30 '19 at 20:04
  • I'm very sorry, but I solved this problem already. – andrey.shedko Dec 31 '19 at 10:28
  • @andrey.shedko would you like to fill us in on how you resolved this?? – Matt Oestreich Jan 01 '20 at 23:09

1 Answers1

2

Look like just some request parameters were wrong, now it's working:

public static async handle(path: string, token: string, userId: number, fileId: string): Promise<void> {
    try {
        axios.post("https://api.dropboxapi.com/2/files/get_temporary_link", { path }, {
            data: {
                path,
            },
            headers: {
                "Authorization": `Bearer ${token}`,
                "Content-Type": "application/json",
            },
        }).then((data) => FileHandler.readFile(data.data.link, data.data.metadata.name)
            .then(() => FileUtilities.parseFile(data.data.metadata.name, fileId)))
            .catch((err) => logger.error(err));
    } catch (err) {
        logger.error(err);
        UtilsRepository.findSupplierEmail(userId).then(data => {
            throw new MailingError(new MailObjectCannotReadFile(data[0].Email));
        });
    }
} 
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121