2

I'm trying to implement the DropNet library into my Windows Phone app but I'm getting the an error when calling the GetAccessTokenAsync method.

Step 1: Get the oauth token from Dropbox

public void ConnectToDropbox()
{
    _client = new DropNetClient(API_KEY, API_SECRET);
    _client.UseSandbox = true;

    // Get Request Token (oauth token) from Dropbox
    _client.GetTokenAsync(
        (userLogin) =>
        {
            // Authorise app with Dropbox
            var url = _client.BuildAuthorizeUrl();
            browser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(browser_LoadCompleted);
            browser.Navigate(new Uri(url));
        },
        (error) =>
        {
            Debug("Error: GetTokenAsync");
        });

}

This seems to work correctly and returns an oauth authorisation code. The URL which the browser navigates to is https://www.dropbox.com/1/oauth/authorize?oauth_token=TSLEY7lLS8K2Mmnr

Step 2: Convert the oauth token into usable Dropbox API token

void browser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    Debug("Callback URL: " + e.Uri.AbsoluteUri);
    if (e.Uri.AbsolutePath == "/1/oauth/authorize")
    {

        //The User has logged in!
        //Now to convert the Request Token into an Access Token
        _client.GetAccessTokenAsync(
            (response) =>
            {
                Debug("User is logged in");
                LoadContents();
            },
                (error) =>
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        Debug("Error getting access token (GetAccessTokenAsync)");
                        MessageBox.Show(error.Message);
                    });
                });
    }
    else
    {
        //Probably the login page loading, ignore
    }
}

The GetAccessTokenAsync method throws the following exceptions at runtime:

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

The in-app MessageBox displays: Exception of type 'DropNet.Exceptions.DropboxException' was thrown.

All the properties of the DropboxException object are as follows:

Response: RestSharp.RestResponse

Status Code: Unauthorized

Stack Trace:

Data: System.Collections.ListDictionaryInternal

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

Inner Exception:

Type: DropNet.Exceptions.DropboxException

Is the the Status Code Unauthorized relevant? I'm using the appropriate API key and secret provdided by Dropbox.

I'd be grateful if anyone who's experienced similar issues when using DropNet could give me some advice in resolving this problem. Happy to provide any further info if needed.

Thanks in advance,

Andrew

Andrew Ridout
  • 434
  • 3
  • 15
  • Have you looked at the Response property of the DropboxException object? It might provide some insight for you. – dkarzon Apr 13 '13 at 23:05
  • I've edited the original post and included the DropboxException properties. It looks like the **RestSharp.RestResponse** is returning with a status code of **Unauthorized**. – Andrew Ridout Apr 14 '13 at 09:56
  • Do you get any content in the RestResponse explaining what the problem is? – dkarzon Apr 14 '13 at 22:38
  • No, there's no further content in RestResponse. I'm starting to wonder if the problem is in the `GetTokenAsync` method. The `BuildAuthorizeURL` method returns the URL: https://www.dropbox.com/1/oauth/authorize?oauth_token=Yopwrkzfd41fpHV1 Is this correct or should the URL also contain an oauth_secret segment? – Andrew Ridout Apr 17 '13 at 18:08
  • I think you are calling the GetAccessToken function too early as you have this line: `if (e.Uri.AbsolutePath == "/1/oauth/authorize")` which is checking for the authorize page where the user allows access to the app (after logging in) which you need to wait for then get the access token. Try using a callback Url here and checking for that. – dkarzon Apr 18 '13 at 00:18
  • Thanks for your advice. I created a new Visual Studio solution and rewrote all the code and it's now working perfectly. DropNet seems like a great library. I'll post my app on your website once it's finished. – Andrew Ridout Apr 18 '13 at 15:56

0 Answers0