Uploading file to Dropbox with Devdefined OAuth li

2019-05-31 02:49发布

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.

1条回答
可以哭但决不认输i
2楼-- · 2019-05-31 03:42

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

查看更多
登录 后发表回答