     expose.launchDropBoxChooser = () ->
         slog 'launchDropBoxChooser'
         # Get access token first so we can convert the link to a path later
-        if not dbx.accessToken
-            window.location = dbx.getAuthenticationUrl(location.href.split("#")[0], 'CHOOSE')
-        else
+        ensureDropboxToken(CHOOSE).then () ->
             Dropbox.choose options =
                 extensions: ['text', '.taskpaper', '.txt', '.ft']
                 success: (files) ->

# To temporarily introspect variables during development.
spy = window
slog = console.log # spy + log

import { DockPanel }                 from 'phosphor-dockpanel'
import { CodeMirrorWidget }          from 'phosphor-codemirror'

    authenticationUrl = dbx.getAuthenticationUrl(location.href.split("#")[0], path)
    authenticationLink = "<a href='#{authenticationUrl}'>#{authenticationUrl}</a>"

    ensureDropboxToken = (state) =>
        authenticationUrl = dbx.getAuthenticationUrl(location.href.split("#")[0], state)

        promise = new Promise (resolve, reject) ->
            if not dbx.accessToken
                window.location = authenticationUrl
                reject(authenticationUrl)

            # test token with API call
            dbx.usersGetCurrentAccount()
                .then (accountData) ->
                    resolve(accountData)
                .catch (error) ->
                    delete localStorage.accessToken
                    window.location = authenticationUrl
                    reject(error)

    # User chose not to give access to Dropbox account
    dropboxAccessDenied = hashKeys.error is 'access_denied'
    if dropboxAccessDenied
