Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 400 when trying to access Dropbox API via Dropbox Javascript SDK

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);
        }
    }
}
like image 457
andrey.shedko Avatar asked Dec 28 '19 10:12

andrey.shedko


People also ask

How do I access Dropbox API?

You can do this by navigating to dropbox.com/developers and selecting the App Console. After you create your account, log in and select Create app. First, select your API. Choosing scoped access for the API gives greater control over the privacy, as well as security of your files and folders.

What API does Dropbox use?

Dropbox API v2. The Dropbox API allows developers to work with files in Dropbox, including advanced functionality like full-text search, thumbnails, and sharing. The Dropbox API explorer is the easiest way to get started making API calls.


1 Answers

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));
        });
    }
} 
like image 57
andrey.shedko Avatar answered Sep 22 '22 16:09

andrey.shedko