如何使用 Casablanca 在 Dropbox API 中调用 /files (POST)

How to use Casablanca to call /files (POST) in Dropbox API

本文关键字:files POST 调用 Casablanca 何使用 Dropbox API      更新时间:2023-10-16

我正在使用 c++ 和 Casablanca 为我们的软件添加 Dropbox 功能。

我可以使用 OAuth 2 登录、获取元数据、打开文件并使用 files_put 成功保存文件。但是,我无法弄清楚如何使用/files(POST)保存文件。

正在使用类似于下面的代码:(即我删除了一些函数来显示我最终得到的硬编码字符串)

{
    using concurrency::streams::file_stream;
    using concurrency::streams::basic_istream;
    utility::string_t strURI = L"https://api-content.dropbox.com/1/files/dropbox/";
    uri url(uri::encode_uri(strURI));
    utility::string_t sb = url.to_string();
    sb += L"?oauth_consumer_key=" + consumerKey + L"&";
    sb += L"oauth_nonce=" + nonce + L"&";       
    sb += L"oauth_timestamp=" + timestamp + L"&";   
    sb += L"oauth_version=2.0";
    sb += L"&access_token=" + accessToken;
    sb += L"&file=" + strFilename; // I've tried with and without this line
    return file_stream<unsigned char>::open_istream(strPath)
        .then([sb, url, &bRet](task<basic_istream<unsigned char>> previousTask)
    {
        try
        {
            auto fileStream = previousTask.get();
            //get the content length, used to set the Content-Length property
            fileStream.seek(0, std::ios::end);
            auto length = static_cast<size_t>(fileStream.tell());
            fileStream.seek(0, 0);
            // Make HTTP request with the file stream as the body.                      
            http_request req;
            http_client client(sb);
            req.set_body(fileStream);
            req.set_method(methods::POST);
            return client.request(req)
                .then([fileStream, &bRet](task<http_response> previousTask)
            {
                // Process response
            }
        }
    };
}

我收到错误的请求响应。我认为问题是我没有正确给它文件名参数,但我不知道它应该去哪里。或者也许我完全错过了别的东西。

任何人都可以帮我澄清这一点吗?

附带说明一下,我认为您正在使用 OAuth 2,但您正在构建一个类似于 OAuth 1 签名的签名。我相信你可以这样做:

utility::string_t sb = url.to_string();
sb += L"?access_token=" + accessToken;

尽管将访问令牌放在身份验证标头中会稍微可取一些:Authorization: Bearer <access token> .

我不是 100% 确定,但我相信/files (POST)需要一个多部分形式编码的正文(因此文件名来自正文中的附件)。我不确定如何在卡萨布兰卡实现这一目标。但我真的会坚持/files_put Dropbox 并仅对需要它的平台使用POST请求(也许他们有更简单的接口来调用)。