forked from serverless/examples
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
42 lines (38 loc) · 1.28 KB
/
handler.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
import request from 'request';
import btoa from 'btoa';
//User Authorization to get a code
export const step1 = (data_ , context , callBack) => {
const state = Math.round(Math.random() * 10);
const url = `https://www.dropbox.com/1/oauth2/authorize?client_id=${process.env.CLIENT_ID}&response_type=code&redirect_uri=${process.env.CALLBACK_URL}&state=${state}`;
callBack(null,{
statusCode : 302 ,
headers : {
location : url
}
})
}
//get a access token to make requests to the api (Make Request From Postman)
export const step2 = (data_ , context , callBack ) => {
const base64 = btoa(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`)
const options = {
method: 'POST',
url: 'https://api.dropbox.com/1/oauth2/token',
headers:
{
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache',
Authorization: `Basic ${base64}`,
},
form:
{ code: data_.queryStringParameters.code,
grant_type: 'authorization_code',
redirect_uri: process.env.CALLBACK_URL
}
};
request(options, function (error, response, body) {
callBack(null , {
statusCode : 200 ,
body
})
});
}