Search code examples
c#dropboxunhandled-exceptiondropnet

Error in dropNet's DropNetClient.GetAccessToken (Unhandled exception)


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 = "[email protected]", Secret = "myPass" };

Solution

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