-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
dropbox.html
48 lines (44 loc) · 1.69 KB
/
dropbox.html
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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/Dropbox-sdk.min.js"></script>
<title>FMG Dropbox Auth</title>
</head>
<body>
<script>
// open this page in a new window without query parameter to start auth
// setDropBoxToken(token) will be called on the opener window
const REDIRECT_URI = window.location.origin + window.location.pathname;
const auth = new Dropbox.DropboxAuth({clientId: "pdr9ae64ip0qno4"});
const params = new URLSearchParams(window.location.search);
const code = params.get("code");
const error = params.get("error");
if (code) getToken();
else if (error) window.opener.Cloud.providers.dropbox.returnError(params.get("error_description"));
else startAuth();
function startAuth() {
auth
.getAuthenticationUrl(REDIRECT_URI, undefined, "code", "offline", undefined, undefined, true)
.then(authUrl => {
window.sessionStorage.clear();
window.sessionStorage.setItem("codeVerifier", auth.codeVerifier);
window.location.href = authUrl;
})
.catch(error => console.error(error));
}
function getToken() {
auth.setCodeVerifier(window.sessionStorage.getItem("codeVerifier"));
auth
.getAccessTokenFromCode(REDIRECT_URI, code)
.then(resp => {
const token = resp.result.access_token;
window.opener.Cloud.providers.dropbox.setDropBoxToken(token);
})
.catch(error => {
console.error(error);
});
}
</script>
</body>
</html>