cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: can't authenticate with javascript

can't authenticate with javascript

Hein B.
New member | Level 1

I try to use the datastore API with javascript. This is my Code:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
        <meta charset="UTF-8">
    </head>
    <body>

        <script src="https://www.dropbox.com/static/api/dropbox-datastores-1.2-latest.js" type="text/javascript"></script>
        <script>
            var client = new Dropbox.Client({key: 'xxxxxxxxxxxx' });

            // Try to finish OAuth authorization.
            client.authenticate({interactive: false}, function (error) {
                if (error) {
                    alert('Authentication error: ' + error);
                }
            });

            alert(client.isAuthenticated());

            if (client.isAuthenticated()) {
                // Client is authenticated. Display UI.
            }
        </script>
    </body>
</html>

The line alert(client.isAuthenticated()); always gives false which means that something went wrong with the authentication. Can someon tell me what I'm doing wrong?

3 Replies 3

Steve M.
Dropbox Staff

The code I see here should all work once authentication has been started, but I don't see a call to start authentication.

Somewhere (e.g. on a button click) should be a call to client.authenticate(function (error) { ... }); (no { interactive: false }). That will start the authentication flow, and then when the user returns to the page, your existing code should finish the auth process.

For an example, see https://www.dropbox.com/developers/datastore/tutorial/js#linking-accounts.

Hein B.
New member | Level 1

Why should I use a button to authenticate? I copied the code from the page that you have linked to and it should work fine. Do I have to do anything else to start authentication or does it suffice to call the method call client.authenticate?

Steve M.
Dropbox Staff

You don't need to call client.authenticate from a button handler (unless you're using a popup, which will get blocked unless invoked from a user action), but it's the typical way to build an app.

You don't need to do anything other than call client.authenticate (without { interactive: false }) to start the authentication process. I'd suggest following the tutorial exactly until you get working code.

Need more support?