Forum Discussion

Continuous I.'s avatar
Continuous I.
New member | Level 1
11 years ago

CSharp SDK dbx.Files.UploadAsync doesn't work

Hi,

I've installed the CSharp DropBox SDK in a Console App (in part of my CI toolchain, although created in Visual Studio 2013 it is sitting in a Xamarin solution that is built using Jenkins on Mac using .Net v4.0.30319).

I call the dropbox SDK immediately after successfully copying an iPhone .IPA to a temp directory so that I can upload it to dropbox, however part of the following does not work:

LogMessage("     :     In UploadToDropBox(...)");

using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))

{

    LogMessage("     :     In using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))");

    try

    {

        var updated = await dbx.Files.UploadAsync(

            folder + "/" + file,

            WriteMode.Overwrite.Instance,

            body: mem);

    }

    catch(Exception ex)

    {

        LogMessage(string.Format("     :     ERROR: {0}", ex.Message));

    }

    LogMessage("     :     Processed var updated = await dbx.Files.UploadAsync(...)");

    LogMessage("     :     Exiting  using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))");

}

The first two Log messages get written to my log file, however none of the other messages do, nor does the try-catch throw and cause that message to be written to the log file.

The value in folder is "test", and the value in file is "test.txt", and the value of content is "Hello DropBox".

But the results are not written to dropbox, despite that the folder exists.

Any ideas on how to resolve this CSharp SDK issue?

Anthony

5 Replies

Replies have been turned off for this discussion
  • I suspect that the error is that your path doesn't start with a slash. In API v2, paths should either be empty, meaning the root of the user's Dropbox, or they should start with a slash. So try using "/test" as the value of folder.

    As to why you don't see the exception, my C# async/await knowledge is somewhat limited. Is the containing method marked async? I think from just this snippet of code, it may not be possible to tell if the exception should get caught here or not.

  • Continuous I.'s avatar
    Continuous I.
    New member | Level 1
    11 years ago

    Hi Steve, thanks for your tip, I've tried the forward slash but it doesn't work.

    The primary call is essentially straight from the CSharp SDK, which is:

    using (var dbx = new DropboxClient("My_Key"))

    {

        await UploadToDropBox(dbx, "/test", "test.txt", "Hello Dropbox"); // tried with /test and just test

    }

    And the method it calls (exactly from the SDK with a Console.WriteLine(...) removed: 

    async Task Upload(DropboxClient dbx, string folder, string file, string content)
    {
        using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))
        {
            var updated = await dbx.Files.UploadAsync(
                folder + "/" + file,
                WriteMode.Overwrite.Instance,
                body: mem);
        }
    }

    But, as stated, without results.

    Kind regards,

    Anthony
  • Hi,

    Would you mind providing your static void Main function? That would be helpful for us to figure out how you make the async call.

  • Continuous I.'s avatar
    Continuous I.
    New member | Level 1
    11 years ago

    Thanks Qiming, with a slight adjustment to that, the errors are now indeed throwing properly, I'll put my main function here and then the errors:

    static void Main(string[] args)

    {

        var task = Task.Run((Func<Task>)Program.Run);

        task.Wait();

    }

    static async Task Run()

    {

        CIPostBuildTask postBuild = new CIPostBuildTask();

        await postBuild.Run();

    }

    The following line (which I added to my first post in a try-catch), shows the error that follows:

    var full = await dbx.Users.GetCurrentAccountAsync();

    ERROR: Error in call to API function "users/get_current_account": The given OAuth 2 access token is malformed.

    In addition, the following line throws the following error in it's try-catch statement:

    var updated = await dbx.Files.UploadAsync(

        folder + "/" + file,

        WriteMode.Overwrite.Instance,

        body: mem);

    ERROR: Error in call to API function "files/upload": The given OAuth 2 access token is malformed.

     

  • Continuous I.'s avatar
    Continuous I.
    New member | Level 1
    11 years ago

    Solved it!

    Qiming - thanks, your help caused me to realise I'd used the App Key instead of the OAuth 2 Generated Access Token.

    Steve - thanks, indeed I had to preface the folder with a forward slash as you said as with it, it works, and without it, it generates the error (quite a long one but here for posterity):

    Error in call to API function "files/upload": HTTP Header "Dropbox-API-Arg": path: 'test/text.txt' did not match pattern '/.*'

    Thanks Chaps, it was lovely to see the Dropbox toast informing me that the file had been uploaded!

    Anthony

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!