使用Devdefined OAuth库将文件上传到Dropbox [英] Uploading file to Dropbox with Devdefined OAuth library

查看:100
本文介绍了使用Devdefined OAuth库将文件上传到Dropbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图同时使用Devdefined的OAuth库将文件上传到Dropbox REST Web服务。

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.

推荐答案

未收到错误的原因是请求没有被执行-要执行请求,您需要获取响应-有多种方法可以执行此操作,但通常最简单的方法是使用方法ReadBody()取回文本。

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().

上传文件的内容不能作为查询参数-根据Dropbox REST API,放置请求的整个主体应为文件的内容。

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:


  • 包括根路径 dropbox 或沙盒(根据您的网址中的API),我认为您不见了。如果您的DropBox应用程序已配置了应用程序文件夹,则使用 sandbox。

  • 在使用者上下文中将 UseHeaderForOAuthParameters设置为true,以确保按要求传递OAuth签名等。标头,而不是被编码为表单参数(因为整个主体都是原始数据)。

  • 使用 WithRawContent(byte [] contents)方法将文件的内容添加到

  • 在PUT请求方法链的最后使用 ReadBody()方法,以使请求得以执行。

  • 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.

结果将是一个包含JSON的字符串,该字符串应类似于以下内容:

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"
}

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

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

这是a放置请求:

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 :)

这篇关于使用Devdefined OAuth库将文件上传到Dropbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆