NoxelNyx
NoxelNyx

Reputation: 997

Getting Dropbox Access Token With Dropnet

I'm attempting to implement file uploading to Dropbox on my site. However, I'm having trouble getting the accessToken after the user clicks to authorize my app.

Here is my code to grab the URL, which gets returned to the client to open a new window in Javascript.

[WebMethod]
public String setUpDropboxOA(String uri, Int32 form_id, String user_auth)
{
    var client = new DropNetClient("xxxxxxxxx", "xxxxxxxxx");
    return client.GetTokenAndBuildUrl(uri);
}

And here is my callback function:

[WebMethod]
public void AuthorizeDropboxCallback(String oauth_token)
{
    var client = new DropNetClient("xxxxxxxxx", "xxxxxxxxx");
    var accessToken = client.GetAccessToken();
    var jsonObj = new { oauth_token = accessToken.Token, oauth_secret = accessToken.Secret };
    var JSONAuthorizationData = JsonConvert.SerializeObject(jsonObj);
    saveNotification(form_hash, "Dropbox", JSONAuthorizationData, user_id);
}

And here is the error that I'm getting on client.GetAccessToken():

Exception of type 'DropNet.Exceptions.DropboxException' was thrown.

The documentation of DropNet says that there is an overload to GetAccessToken that will allow you to specify a token to use, however, I'm not seeing one. I feel like this is the problem here, but I'm not entirely sure.

Upvotes: 0

Views: 1570

Answers (2)

dkarzon
dkarzon

Reputation: 8038

As @albattran's answer suggested it is because you are creating 2 different instances of the DropNetClient.

client.GetTokenAndBuildUrl(uri);

Thismethod actually does 2 things under the hood. 1, Makes an API call to Dropbox to get a request token then using that request token it creates the login url.

To solve this you will need a way to store that request token between web requests.

Maybe think about something like the below using the session.

var userToken = client.GetToken();
Session["user_token"] = userToken.Token;
Session["user_secret"] = userToken.Secret;

Then ok the callback read those session variables and add them to the constructor overload of the DropNetClient.

var token = Session["user_token"];
var secret = Session["user_secret"];
var client = new DropNetClient("XXXX", "XXXX", token, secret);
client.GetAccessToken();

Upvotes: 1

albattran
albattran

Reputation: 1907

I think your problem is a result of losing the instance of DropNetClient between the different requests, you are creating two instances of DropNetClient.

You need to persist the initial token form GetTokenAndBuildUrl and restore it when you call GetAccessToken.

Because oAuth is 3 steps:

  1. Get Request Token
  2. Send user for authorization, and get back verifier
  3. Get Access token using the original Request Token and the verifier

Upvotes: 0

Related Questions