-
Notifications
You must be signed in to change notification settings - Fork 351
/
code_flow_example.js
52 lines (43 loc) · 1.46 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
// 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
const fetch = require('node-fetch');
const app = require('express')();
const hostname = 'localhost';
const port = 3000;
const config = {
fetch,
clientId: 'jg8wc1hfkvel6ql',
clientSecret: 'f0i5w4e6mlbbme5',
};
const { Dropbox } = require('dropbox'); // eslint-disable-line import/no-unresolved
const dbx = new Dropbox(config);
const redirectUri = `http://${hostname}:${port}/auth`;
app.get('/', (req, res) => {
dbx.auth.getAuthenticationUrl(redirectUri, null, 'code', 'offline', null, 'none', false)
.then((authUrl) => {
res.writeHead(302, { Location: authUrl });
res.end();
});
});
app.get('/auth', (req, res) => { // eslint-disable-line no-unused-vars
const { code } = req.query;
console.log(`code:${code}`);
dbx.auth.getAccessTokenFromCode(redirectUri, code)
.then((token) => {
console.log(`Token Result:${JSON.stringify(token)}`);
dbx.auth.setRefreshToken(token.result.refresh_token);
dbx.usersGetCurrentAccount()
.then((response) => {
console.log('response', response);
})
.catch((error) => {
console.error(error);
});
})
.catch((error) => {
console.log(error);
});
});
app.listen(port);