-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_flow_example.js
52 lines (39 loc) · 1.16 KB
/
code_flow_example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
// Standalone example to demonstrate codeflow.
// Start the server, hit localhost:3000 on the browser, and click through.
// On the server logs, you should have the auth code, as well as the token
// from exchanging it. This exchange is invisible to the app user
var fetch = require('isomorphic-fetch');
const app = require('express')();
const hostname = 'localhost';
const port = 3000;
//const https = require('https');
const config = {
fetch: fetch,
clientId: [clientId],
clientSecret: [clientSecret]
};
const Dropbox = require('dropbox').Dropbox;
var dbx = new Dropbox(config);
const redirectUri = `http://${hostname}:${port}/auth`;
const authUrl = dbx.getAuthenticationUrl(redirectUri, null, 'code');
app.get('/', (req, res) => {
res.writeHead(302, { 'Location': authUrl });
res.end();
});
app.get('/auth', (req, res) => {
let code = req.query.code;
console.log(code);
var options = Object.assign({
code,
redirectUri
}, config);
dbx.getAccessTokenFromCode(redirectUri, code)
.then(function(token) {
console.log(token);
})
.catch(function(error) {
console.log(error);
});
});
app.listen(port);