Skip to content

Commit 1955286

Browse files
author
Eric Koleda
authored
Add sample for Dropbox
1 parent 70cfb2b commit 1955286

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

samples/Dropbox.gs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Dropbox API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.dropboxapi.com/1/account/info';
11+
var response = UrlFetchApp.fetch(url, {
12+
headers: {
13+
Authorization: 'Bearer ' + service.getAccessToken()
14+
}
15+
});
16+
var result = JSON.parse(response.getContentText());
17+
Logger.log(JSON.stringify(result, null, 2));
18+
} else {
19+
var authorizationUrl = service.getAuthorizationUrl();
20+
Logger.log('Open the following URL and re-run the script: %s',
21+
authorizationUrl);
22+
}
23+
}
24+
25+
/**
26+
* Reset the authorization state, so that it can be re-tested.
27+
*/
28+
function reset() {
29+
var service = getService();
30+
service.reset();
31+
}
32+
33+
/**
34+
* Configures the service.
35+
*/
36+
function getService() {
37+
return OAuth2.createService('Dropbox')
38+
// Set the endpoint URLs.
39+
.setAuthorizationBaseUrl('https://www.dropbox.com/1/oauth2/authorize')
40+
.setTokenUrl('https://api.dropboxapi.com/1/oauth2/token')
41+
42+
// Set the client ID and secret.
43+
.setClientId(CLIENT_ID)
44+
.setClientSecret(CLIENT_SECRET)
45+
46+
// Set the name of the callback function that should be invoked to complete
47+
// the OAuth flow.
48+
.setCallbackFunction('authCallback')
49+
50+
// Set the property store where authorized tokens should be persisted.
51+
.setPropertyStore(PropertiesService.getUserProperties())
52+
53+
// Set the response type to code (required).
54+
.setParam('response_type', 'code');
55+
}
56+
57+
/**
58+
* Handles the OAuth callback.
59+
*/
60+
function authCallback(request) {
61+
var service = getService();
62+
var authorized = service.handleCallback(request);
63+
if (authorized) {
64+
return HtmlService.createHtmlOutput('Success!');
65+
} else {
66+
return HtmlService.createHtmlOutput('Denied');
67+
}
68+
}
69+
70+
/**
71+
* Logs the redict URI to register in the Dropbox application settings.
72+
*/
73+
function logRedirectUri() {
74+
var service = getService();
75+
Logger.log(service.getRedirectUri());
76+
}

0 commit comments

Comments
 (0)