0

I'm making a dropBox downloader in .Net and getting unhandled exception every time i try to run the code. I already authenticated the dropbox App.

Here's the code:

dropClient = new DropNetClient("xxxxxxxxxxxxx", "yyyyyyyyyyyyyy");
//Btw in my code I have the actual app key and secret
dropClient.GetToken();
var accessToken = dropClient.GetAccessToken();
dropClient.UserLogin = new DropNet.Models.UserLogin { Token = "myemail@gmail.com", Secret = "myPass" };
Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
Morga121
  • 3
  • 6
  • What's the actual exception? By the way, you shouldn't be programmatically using your username and password with the Dropbox API. App authorization should only be processed via an OAuth flow, resulting in an access token you can use without the username and password. – Greg May 31 '15 at 19:39
  • The exception is an DropboxRestException in DropNet.dll and thx – Morga121 Jun 01 '15 at 06:56
  • That sounds like a relatively general exception. Can you post the full output? – Greg Jun 01 '15 at 07:36

1 Answers1

1

The problem here is that you're not actually getting the client credentials

Here is my dropnet connection manager class

public class ConnectionManager
{
    private DropNetClient _client;


    public string GetConnectUrl(DropNetClient client, string callbackurl)
    {
        _client = client;

        var url = _client.BuildAuthorizeUrl(callbackurl);

        return url;
    }


    public DropNetClient Connect()
    {
        _client = new DropNetClient("token", "secret");

        _client.GetToken();

        return _client;
    }


    public Dictionary<string, string> GetAccessToken(string tok, string secret)
    {

        _client = new DropNetClient("token", "secret", tok, secret);
        var token = _client.GetAccessToken();
        var dic = new Dictionary<string, string> {{"token", token.Token}, {"secret", token.Secret}};
        return dic;
    }

    public DropNetClient Connect(TokenAndSecretModel model)
    {

        _client = new DropNetClient("token", "secret", model.Token, model.Secret);

        var info = _client.AccountInfo();

        return _client;
    }

}

First call Connect() Then GetConnectUrl(...) Your callback url is needed to know when the call is complete. Redirect to the URL returned, then wait for your callback url to receive a response. When it does call GetAccessToken(token, secret) where token and secret are from _client.UserLogin.Token and _client.UserLogin.Secret GetAccessToken will then return the Token and Secret that you need to save

To complete this, here is my controller

public class DropBoxController : Controller
{

    private readonly ICommandChannel _commandChannel;
    private readonly IQueryChannel _queryChannel;
    private readonly UserModel _user;

    public DropBoxController(ICommandChannel commandChannel, IQueryChannel queryChannel, UserModel user)
    {
        _commandChannel = commandChannel;
        _queryChannel = queryChannel;
        _user = user;
    }
    public ActionResult Index()
    {
        var con = new ConnectionManager();
        var dropclient = con.Connect();
        var callbackurl = Request.Url.Scheme + "://" + Request.Url.Authority + "/DropBox/Callback";
        var url = con.GetConnectUrl(dropclient, callbackurl);
        _commandChannel.Execute(new SaveDropBoxTempSecurityCommand { AuthToken = dropclient.UserLogin.Token, Token = dropclient.UserLogin.Token, Secret = dropclient.UserLogin.Secret });

        return Redirect(url);
        //return View(new UrlModel {Url = url});

    }

    [HttpGet]
    public ActionResult Callback(string oauth_token)
    {
        TokenAndSecretModel model = _queryChannel.Execute(new GetDropBoxTempTokenQuery{Token = oauth_token});
        var con = new ConnectionManager();
        var login = con.GetAccessToken(model.Token, model.Secret);
        _commandChannel.Execute(new SaveDropBoxLoginCommand{AuthToken = oauth_token, Login = login});
        return View();
    }


}
public class UrlModel
{
    public string Url { get; set; }
}

You can see I persist the temporary credentials to the database, you could if you wanted just save them to the session. I hope this helps.

sheavens
  • 685
  • 7
  • 14