2

My goal is to get files by DropBoxRest API in C#

I'm getting the following error when I want to get the token

An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll

What I do wrong ?

Here is my code

    var options = new Options
{
ClientId = "", //App key
ClientSecret = "", //App secret
RedirectUri = "https://www.dropbox.com/1/oauth2/authorize"
};
        // Initialize a new Client (without an AccessToken)
        var client = new Client(options);

        // Get the OAuth Request Url
        var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync("");

        // TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
        var authCode = ""; Which code have to be here ???

        // Exchange the Authorization Code with Access/Refresh tokens
        var token = await client.Core.OAuth2.TokenAsync(authCode); in this line occured the following error
          //An unhandled exception of type 'System.AggregateException' occurred in mscorlib.dll      

        // Get account info
        var accountInfo = await client.Core.Accounts.AccountInfoAsync();
6
  • 1
    at which point you're getting error in above code? Commented Mar 3, 2015 at 13:28
  • In this point var token = await client.Core.OAuth2.TokenAsync(authCode); Commented Mar 3, 2015 at 13:31
  • how are you getting authCode? Commented Mar 3, 2015 at 13:34
  • I don't know how I have to get the authCode :) I think that's the reason of the error Commented Mar 3, 2015 at 13:38
  • You code is showing that you're making request on dropbox server for some reason. You should read their API to know how to obtain proper authCode. Commented Mar 3, 2015 at 13:43

2 Answers 2

4

You appear to be using my library, so I'll try to respond.

The Dropbox API uses the OAuth 2.0 flow, which means that you need to redirect your users to a Dropbox page for authentication, and for approving you app to access their data.

After making the call to AuthorizeAsync (notice the "code" argument):

var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync("code");

You will receive a URL that you can redirect your users to. That URL will open a page in Dropbox for authentication and approval. After that, the users will be redirected back to the URL you provided in RedirectUri in the options. The redirect will include the code in the query string of the URL.

This means that you should have some server, that will listen to your RedirectUri. For example:

var options = new Options
    {
        ClientId = "", //App key
        ClientSecret = "", //App secret
        RedirectUri = "https://www.myserver.com/Dropbox/SetCode"
    };

And if you are using MVC, you might have an Action in a Controller as follow:

public class DropboxController : Controller
{
    public ActionResult SetCode(string code, string error) {}
}

Once you retrieve the code, you can call TokenAsync():

var token = await client.Core.OAuth2.TokenAsync(authCode);

Your code is based on the sample in my library, which expect you to manually copy the authRequestUrl, open it in a browser, and then manually retrieve the code.

Note: There are several methods to work without a server, but those are out of scope for the library itself. I might look to include them if there's enough demand.

2
  • I am among those who need to authenticate without a server (for a Windows Store app), so here is my vote for that enhancement!
    – Ty Jacobs
    Commented Apr 23, 2015 at 12:30
  • @TyJacobs you can see a sample (using WPF, but I'm sure you'll be able to understand it) from my OneDriveRestAPI github.com/saguiitay/OneDriveRestAPI
    – SaguiItay
    Commented Apr 24, 2015 at 14:37
0

First of all you can get more details on this error, when you catch the exception:

try
{
    var token = await client.Core.OAuth2.TokenAsync(authCode);
}
catch (AggregateException aex)
{
    // set a breakpoint on the opening curly brace and check the
    // variable "aex".
}

The second thing is, that you need a combination of secrets for doing OAuth. As mentioned here you need something to identify the yourself resp. your app against an OAuth-provider. For this you need to provide some sort of key which is provided by the OAuth-provider to you.

At Dropbox you need to configure everything through the console.

EDIT

You have to generate an access token at DropBox AppConsole which you can pass directly to the client options:

var options = new Options
{
    ClientId = "{see console}", //App key
    ClientSecret = "{see console}", //App secret
    AccessToken = "{see console}",
    RedirectUri = "{see console}"
};
var client = new Client(options);

In this case you don't need to get the token by default.

I debugged it down due to the negative voting. I can get the exception-details saying the following:

DropboxRestAPI.Models.Exceptions.ServiceErrorException was unhandled Message: An unhandled exception of type 'DropboxRestAPI.Models.Exceptions.ServiceErrorException' occurred in mscorlib.dll Additional information: invalid_grant

The JavaScript-documentation of DropBox states that this indicates a wrong API possibly.

8
  • By your code I can't see the error message because error doesn't enter into catch statement Commented Mar 3, 2015 at 13:41
  • He's not passing authCode properly, hence getting error. What else is required to know? Commented Mar 3, 2015 at 13:42
  • My question is that. What I do wrong and how I can get authCode Commented Mar 3, 2015 at 13:51
  • you should read dropbox api documentation for getting authCode Commented Mar 3, 2015 at 13:59
  • How about var authRequestUrl = await client.Core.OAuth2.AuthorizeAsync(""); ? What I need to pass in AuthorizeAsync? Commented Mar 3, 2015 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.