尝试通过Dropbox Javascript SDK访问Dropbox API时出现错误400

时间:2019-12-28 10:34:07

标签: javascript node.js dropbox-api

我有一个通过Dropbox Javascript SDK尝试下载文件的应用程序。我不知道怎么了。通过提取调用直接访问Dropbox API会带来相同的错误。 Dropbox API文档说错误400是针对错误的输入参数的,而我似乎可以发送-“ 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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好像只是一些请求参数是错误的,现在可以了:

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