Question

I am trying to use the Dropbox SDK for Javascript, so I tried a Hello World! to familiarize myself, but I'm getting an error 401 for failed authentication.

Printscreen of the error I am getting

When I close the pop-up, I click on the button and it redirects me to the authorization page, but after I click on "Allow", it'll show a "Corrupted Content Error" page.

Here's my code, which I really got from a tutorial page in order to test it out:

<!doctype html>
<html>
<head>
    <script src="https://www.dropbox.com/static/api/dropbox-datastores-1.0-latest.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <center>
        <button id="writeButton">Click to create <code>hello.txt</code> in Dropbox.</button>
    </center>`

    <script>
        var client = new Dropbox.Client({
            key: KEY// //APP KEY
            secret: "<REDACTED>"//"<REDACTED>"          //APP SECRET
        });

        function doHelloWorld() {
            client.writeFile('hello.txt', 'Hello, World!', function (error) {
                if (error) {
                    alert('Error: ' + error);
                } else {
                    alert('File written successfully!');
                }
            });
        }

        // Try to complete OAuth flow.
        client.authenticate({ interactive: false }, function (error) {
            if (error) {
                alert('Error: ' + error);
            } else {
                doHelloWorld();
            }
        });

        document.getElementById('writeButton').onclick = function () {
            client.authenticate(function (error, client) {
                if (error) {
                    alert('Error: ' + error);
                } else {
                    doHelloWorld();
                }
            });
        }
    </script>
</body>
</html>
Was it helpful?

Solution

EDIT

It looks like the code I posted on the Dropbox dev blog doesn't work (now?). You need to manually check whether the client is authenticated, since the callback from client.authenticate is called no matter what.

I'll update the blog post. The fixed code should look like this:

// Try to complete OAuth flow.
client.authenticate({ interactive: false }, function (error) {
    if (error) {
        alert('Error: ' + error);
    } else if (client.isAuthenticated()) { // <-- this line changed
        doHelloWorld();
    }
});

Original (wrong) answer

At first glance, the only difference I can spot between your code and mine (https://www.dropbox.com/developers/blog/71/writing-a-file-with-the-dropbox-javascript-sdk) is that you're specifying an app secret (when you shouldn't be).

If you remove that line, does your app work?

(BTW, I redacted the app secrets you posted, but they're still visible in the edit history. I'd recommend deleting those apps and creating new ones.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top