使用 Devdefined OAuth 库将文件上传到 Dropbox

发布于 2024-12-23 09:34:02 字数 449 浏览 5 评论 0原文

我正在尝试将文件上传到 Dropbox REST Web 服务,同时使用 Devdefine 的 OAuth 库。

这是我正在使用的方法:

    public static void UploadFile(string filenameIn, string directoryIn, byte[] dataIn)
    {
        DropBox.session.Request().Put().ForUrl("https://api-content.dropbox.com/1/files_put/" + directoryIn + filenameIn)
            .WithQueryParameters(new { param = dataIn });
    }

该方法似乎没有执行任何操作,也没有抛出任何异常。输出也没有错误。我尝试使用断点来确认它也在调用代码。

I am trying to upload a file to the Dropbox REST web service while at the same time using Devdefined's OAuth library.

This is the method I'm using:

    public static void UploadFile(string filenameIn, string directoryIn, byte[] dataIn)
    {
        DropBox.session.Request().Put().ForUrl("https://api-content.dropbox.com/1/files_put/" + directoryIn + filenameIn)
            .WithQueryParameters(new { param = dataIn });
    }

The method doesn't appear to do anything, nor throw any exceptions. There's no errors in the output either. I've tried using breakpoints to confirm it's calling the code as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-12-30 09:34:02

您没有收到错误的原因是因为请求没有被执行 - 要执行请求,您需要获取响应 - 有多种方法可以做到这一点,但通常最简单的就是使用以下命令取回文本ReadBody() 方法。

上传文件的内容不能作为查询参数来完成 - 根据 dropbox REST API,放置请求的整个正文应该是文件的内容。

本质上,要使这一切正常工作,您需要:

  • 根据您的 URL 中的 API 包含根路径“dropbox”或“sandbox”,我认为您丢失了。如果您的 DropBox 应用程序已配置了应用程序文件夹,则您可以使用“沙箱”。
  • 在消费者上下文中将“UseHeaderForOAuthParameters”设置为 true,以确保 OAuth 签名等作为请求标头传递,而不是编码为表单参数(因为整个正文都是原始数据)。
  • 使用“WithRawContent(byte[]contents)”方法将文件的内容添加到请求中。
  • 使用 PUT 请求方法链末尾的“ReadBody()”方法来执行请求。

结果将是一个包含 JSON 的字符串,应如下所示:

{
  "revision": 5, 
  "rev": "5054d8c6e", 
  "thumb_exists": true, 
  "bytes": 5478,
  "modified": "Thu, 29 Dec 2011 10:42:05 +0000",
  "path": "/img_fa06e557-6736-435c-b539-c1586a589565.png", 
  "is_dir": false, 
  "icon": "page_white_picture",
  "root": "app_folder",
  "mime_type": "image/png",
  "size": "5.3KB"
}

我已在 github 上的 DevDefined.OAuth-examples 项目中添加了一个示例,该示例演示了如何使用 DropBox 执行 GET 和 PUT 请求:

https://github.com/bittercoder/DevDefined.OAuth-Examples/blob/master/src/ExampleDropBoxUpload/Program.cs

这是放置请求特别需要的代码:

var consumerContext = new OAuthConsumerContext
{
    SignatureMethod = SignatureMethod.HmacSha1,
    ConsumerKey = "key goes here",
    ConsumerSecret = "secret goes here", 
    UseHeaderForOAuthParameters = true
};

var session = new OAuthSession(consumerContext, "https://api.dropbox.com/1/oauth/request_token",
   "https://www.dropbox.com/1/oauth/authorize",
   "https://api.dropbox.com/1/oauth/access_token");

IToken requestToken = session.GetRequestToken();

string authorisationUrl = session.GetUserAuthorizationUrlForToken(requestToken);

Console.WriteLine("Authorization Url: {0}", authorisationUrl);

// ... Authorize request... and then...

session.ExchangeRequestTokenForAccessToken(requestToken);

string putUrl = "https://api-content.dropbox.com/1/files_put/sandbox/some-image.png";

byte[] contents = File.ReadAllBytes("some-image.png");

IConsumerRequest putRequest = session.Request().Put().ForUrl(putUrl).WithRawContent(contents);

string putInfo = putRequest.ReadBody();

Console.WriteLine("Put response: {0}", putInfo);

希望应该把事情弄清楚一点,不幸的是,没有文档,仅通过查看源代码来弄清楚这些事情有点棘手:)

The reason you are not receiving an error is because the request is not being executed - to execute the request you need to fetch the response - there is a number of ways to do this, but often the simplest is just to fetch the text back using the method ReadBody().

Uploading the contents of a file can not be done as a query parameter - as per the dropbox REST API, the entire body of the put request should be the file's contents.

Essentially for this all to work you will need to:

  • Include the root path "dropbox" or "sandbox" as per the API in your Url, which I think you are missing. You use "sandbox" if your DropBox application has been configured with an application folder.
  • Set "UseHeaderForOAuthParameters" to true in the consumer context, to ensure the OAuth signature etc. is passed as request headers, as opposed to being encoded as form parameters (because the entire body is raw data).
  • Use the "WithRawContent(byte[] contents)" method to add the file's contents to the request.
  • Use the "ReadBody()" method at the very end of the PUT request method chain, to cause the request to be executed.

The result will be a string containing JSON that should look something like this:

{
  "revision": 5, 
  "rev": "5054d8c6e", 
  "thumb_exists": true, 
  "bytes": 5478,
  "modified": "Thu, 29 Dec 2011 10:42:05 +0000",
  "path": "/img_fa06e557-6736-435c-b539-c1586a589565.png", 
  "is_dir": false, 
  "icon": "page_white_picture",
  "root": "app_folder",
  "mime_type": "image/png",
  "size": "5.3KB"
}

I've added an example to the DevDefined.OAuth-examples project on github that demonstrates how to do GET and PUT requests with DropBox:

https://github.com/bittercoder/DevDefined.OAuth-Examples/blob/master/src/ExampleDropBoxUpload/Program.cs

And here's the code specifically required for a put request:

var consumerContext = new OAuthConsumerContext
{
    SignatureMethod = SignatureMethod.HmacSha1,
    ConsumerKey = "key goes here",
    ConsumerSecret = "secret goes here", 
    UseHeaderForOAuthParameters = true
};

var session = new OAuthSession(consumerContext, "https://api.dropbox.com/1/oauth/request_token",
   "https://www.dropbox.com/1/oauth/authorize",
   "https://api.dropbox.com/1/oauth/access_token");

IToken requestToken = session.GetRequestToken();

string authorisationUrl = session.GetUserAuthorizationUrlForToken(requestToken);

Console.WriteLine("Authorization Url: {0}", authorisationUrl);

// ... Authorize request... and then...

session.ExchangeRequestTokenForAccessToken(requestToken);

string putUrl = "https://api-content.dropbox.com/1/files_put/sandbox/some-image.png";

byte[] contents = File.ReadAllBytes("some-image.png");

IConsumerRequest putRequest = session.Request().Put().ForUrl(putUrl).WithRawContent(contents);

string putInfo = putRequest.ReadBody();

Console.WriteLine("Put response: {0}", putInfo);

Hopefully that should clear things up a bit, unfortunately without documentation it's a little tricky to figure these things out by just looking at the source code :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文