2

As many people before me, I'm failing miserably to successfully save an access token between launches of my application. This application is actually only going to be used with one specific dropbox account, though I'm trying to make it easy to set up and a bit more dynamic.

Of course, if the settings are empty (initial run), the user can correctly log in and authorize the app, and coming back to the app it works as intended. On subsequent runs, however, when it grabs the token and secret from the settings collection it fails miserably with

Received Response [Unauthorized] : Expected to see [OK]. The HTTP response was [{"error": "Invalid signature."}].

I'm obviously doing something wrong, what is it? Thanks!

Code below!

using System;
using DropNet;

namespace DS_Uploader_DropBox {
    class Program {
        private const string AppKey = "my super secret app key";
        private const string AppSecret = "my super secret app secret";

        static void Main(string[] args) {
            DropNetClient client;
            DropNet.Models.UserLogin token;

            string userToken = Settings.Default.userToken;
            string userSecret = Settings.Default.userSecret;

            bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

            //needAccessToken = true;

            if (needAccessToken) {
                client = new DropNet.DropNetClient(AppKey, AppSecret);
                client.UseSandbox = true;
                client.GetToken();

                // Auth with dropbox
                var url = client.BuildAuthorizeUrl();

                // Prompt for user to auth
                Console.WriteLine("go auth here " + url);
                Console.ReadLine();

                // If the user authed, let's get that token
                try {
                    token = client.GetAccessToken();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }

                // save for later
                userToken = token.Token;
                userSecret = token.Secret;
                Settings.Default.userToken = userToken;
                Settings.Default.userSecret = userSecret;
                Settings.Default.Save();

            } else {
                client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
                client.UseSandbox = true;
                client.GetToken();

                // get that token
                try {
                    token = client.GetAccessToken();
                } catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }
            }

            var acctInfo = client.AccountInfo();
            Console.WriteLine(acctInfo.display_name);
            Console.ReadLine();
        }
    }
}

Code that worked to follow:

using System;
using DropNet;

namespace DS_Uploader_DropBox {
    class Program {
        private const string AppKey = "my super secret app key";
        private const string AppSecret = "my super secret app secret";

        static void Main(string[] args) {
            DropNetClient client;
            DropNet.Models.UserLogin token;

            string userToken = Settings.Default.userToken;
            string userSecret = Settings.Default.userSecret;

            bool needAccessToken = (String.IsNullOrEmpty(userToken) || string.IsNullOrEmpty(userSecret));

            //needAccessToken = true;

            if (needAccessToken) {
                client = new DropNet.DropNetClient(AppKey, AppSecret);
                client.UseSandbox = true;
                client.GetToken();

                // Auth with dropbox
                var url = client.BuildAuthorizeUrl();

                // Prompt for user to auth
                Console.WriteLine("go auth here " + url);
                Console.ReadLine();

                // If the user authed, let's get that token
                try {
                    token = client.GetAccessToken();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception! " + e.Message);
                    return;
                }

                // save for later
                userToken = token.Token;
                userSecret = token.Secret;
                Settings.Default.userToken = userToken;
                Settings.Default.userSecret = userSecret;
                Settings.Default.Save();

            } else {
                client = new DropNet.DropNetClient(AppKey, AppSecret, userToken, userSecret);
                client.UseSandbox = true;
            }

            var acctInfo = client.AccountInfo();
            Console.WriteLine(acctInfo.display_name);
            Console.ReadLine();
        }
    }
}
The1nk
  • 702
  • 14
  • 25
  • 1
    What line fails with that exception? Also, why are you calling `GetToken` and `GetAccessToken` in the code path where `needAccessToken` is false? It looks like you already have an access token (`userToken` and `userSecret`). – Greg Apr 08 '15 at 20:01
  • @Greg .. Yep.. That was my problem. I was trying to `GetToken()` to get a Request token, when I didn't need to, and was trying to `GetAccessToken()` when I didn't need to. So, can you put your comment as an answer? – The1nk Apr 08 '15 at 20:08

1 Answers1

2

In the code path where needAccessToken is false, you're calling GetToken and GetAccessToken again, in an attempt to get a new request token and a new access token, respectively. This is unnecessary, as you already have and retrieved the existing access token in userToken and userSecret.

Greg
  • 16,359
  • 2
  • 34
  • 44