Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At the end of the authentication process, you have an access token, which is what's used to make calls to the API. If both the client and the server need to make calls to the API, then both will need to have the access token.</p> <p>If you're doing the authentication client-side today, you could pull the access token out somehow (not sure if/how it's exposed from the library, but it's in there somewhere and also storaged in local storage) and pass it to the server. The server can then use it to call <a href="https://www.dropbox.com/developers/core/docs#account-info"><code>/account/info</code></a> and get the Dropbox user ID of the authenticated user.</p> <p>An alternative is to do it the other way around. Authenticate the user with the "code flow" (rather than "token flow") and get the access token on the server in the first place. Then you could pass it down to the client and pass it as an option in the <a href="https://www.dropbox.com/developers/datastore/docs/js#Dropbox.Client"><code>Dropbox.Client</code> constructor</a>. I think that <code>dropbox-js</code> supports this itself, but it's also not hard to do yourself. Here's some raw Express code that logs in a user and displays his or her name:</p> <pre><code>var crypto = require('crypto'), express = require('express'), request = require('request'), url = require('url'); var app = express(); app.use(express.cookieParser()); // insert your app key and secret here var appkey = '&lt;your app key&gt;'; var appsecret = '&lt;your app secret&gt;'; function generateCSRFToken() { return crypto.randomBytes(18).toString('base64') .replace(/\//g, '-').replace(/\+/g, '_'); } function generateRedirectURI(req) { return url.format({ protocol: req.protocol, host: req.headers.host, pathname: app.path() + '/callback' }); } app.get('/', function (req, res) { var csrfToken = generateCSRFToken(); res.cookie('csrf', csrfToken); res.redirect(url.format({ protocol: 'https', hostname: 'www.dropbox.com', pathname: '1/oauth2/authorize', query: { client_id: appkey, response_type: 'code', state: csrfToken, redirect_uri: generateRedirectURI(req) } })); }); app.get('/callback', function (req, res) { if (req.query.error) { return res.send('ERROR ' + req.query.error + ': ' + req.query.error_description); } // check CSRF token if (req.query.state !== req.cookies.csrf) { return res.status(401).send( 'CSRF token mismatch, possible cross-site request forgery attempt.' ); } else { // exchange access code for bearer token request.post('https://api.dropbox.com/1/oauth2/token', { form: { code: req.query.code, grant_type: 'authorization_code', redirect_uri: generateRedirectURI(req) }, auth: { user: appkey, pass: appsecret } }, function (error, response, body) { var data = JSON.parse(body); if (data.error) { return res.send('ERROR: ' + data.error); } // extract bearer token var token = data.access_token; // use the bearer token to make API calls request.get('https://api.dropbox.com/1/account/info', { headers: { Authorization: 'Bearer ' + token } }, function (error, response, body) { res.send('Logged in successfully as ' + JSON.parse(body).display_name + '.'); }); // write a file // request.put('https://api-content.dropbox.com/1/files_put/auto/hello.txt', { // body: 'Hello, World!', // headers: { Authorization: 'Bearer ' + token } // }); }); } }); app.listen(8000); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload