Skip to content

Commit 525c4f6

Browse files
author
Eric Koleda
committed
Add new sample for AdobeSign and update Harvest sample to use the new storage framework.
1 parent 734c4fa commit 525c4f6

File tree

2 files changed

+106
-10
lines changed

2 files changed

+106
-10
lines changed

samples/AdobeSign.gs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var CLIENT_ID = '...';
2+
var CLIENT_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Adobe Sign API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
// Retrieve the API access point from storage.
11+
var apiAccessPoint = service.getStorage().getValue('api_access_point');
12+
var url = apiAccessPoint + 'api/rest/v5/users/me';
13+
var response = UrlFetchApp.fetch(url, {
14+
headers: {
15+
Authorization: 'Bearer ' + service.getAccessToken()
16+
}
17+
});
18+
var result = JSON.parse(response.getContentText());
19+
Logger.log(JSON.stringify(result, null, 2));
20+
} else {
21+
if (service.getLastError()) {
22+
Logger.log(service.getLastError());
23+
}
24+
var authorizationUrl = service.getAuthorizationUrl();
25+
Logger.log('Open the following URL and re-run the script: %s',
26+
authorizationUrl);
27+
}
28+
}
29+
30+
/**
31+
* Reset the authorization state, so that it can be re-tested.
32+
*/
33+
function reset() {
34+
var service = getService();
35+
service.reset();
36+
}
37+
38+
/**
39+
* Configures the service.
40+
*/
41+
function getService(opt_apiAccessPoint) {
42+
var service = OAuth2.createService('AdobeSign')
43+
// Set the endpoint URLs.
44+
.setAuthorizationBaseUrl('https://secure.echosign.com/public/oauth')
45+
46+
// Set the client ID and secret.
47+
.setClientId(CLIENT_ID)
48+
.setClientSecret(CLIENT_SECRET)
49+
50+
// Set the name of the callback function that should be invoked to
51+
// complete the OAuth flow.
52+
.setCallbackFunction('authCallback')
53+
54+
// Set the property store where authorized tokens should be persisted.
55+
.setPropertyStore(PropertiesService.getUserProperties())
56+
.setPropertyStore(PropertiesService.getScriptProperties())
57+
58+
// Set the scopes.
59+
.setScope('user_read');
60+
61+
// Set the token and refresh URL using the API access point passed in or
62+
// stored in the token.
63+
var apiAccessPoint = opt_apiAccessPoint ||
64+
service.getStorage().getValue('api_access_point');
65+
if (apiAccessPoint) {
66+
service.setTokenUrl(apiAccessPoint + 'oauth/token');
67+
service.setRefreshUrl(apiAccessPoint + 'oauth/refresh');
68+
}
69+
70+
return service;
71+
}
72+
73+
/**
74+
* Handles the OAuth callback.
75+
*/
76+
function authCallback(request) {
77+
// Get the API access point specified in the URL parameters.
78+
var apiAccessPoint = request.parameter.api_access_point;
79+
var service = getService(apiAccessPoint);
80+
var authorized = service.handleCallback(request);
81+
if (authorized) {
82+
// Save the API access point in the service's storage.
83+
service.getStorage().setValue('api_access_point', apiAccessPoint);
84+
return HtmlService.createHtmlOutput('Success!');
85+
} else {
86+
return HtmlService.createHtmlOutput('Denied');
87+
}
88+
}
89+
90+
/**
91+
* Logs the redict URI to register in the Dropbox application settings.
92+
*/
93+
function logRedirectUri() {
94+
var service = getService();
95+
Logger.log(service.getRedirectUri());
96+
}

samples/Harvest.gs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ var CLIENT_SECRET = '...';
77
function run() {
88
var service = getService();
99
if (service.hasAccess()) {
10-
var accountId = PropertiesService.getUserProperties()
11-
.getProperty('Harvest-Account-Id');
10+
// Retrieve the account ID from storage.
11+
var accountId = service.getStorage().getValue('Harvest-Account-Id');
1212
var url = 'https://api.harvestapp.com/v2/users/me';
1313
var response = UrlFetchApp.fetch(url, {
1414
headers: {
@@ -47,12 +47,13 @@ function getService() {
4747
.setClientId(CLIENT_ID)
4848
.setClientSecret(CLIENT_SECRET)
4949

50-
// Set the name of the callback function that should be invoked to
51-
// complete the OAuth flow.
50+
// Set the name of the callback function that should be invoked to complete
51+
// the OAuth flow.
5252
.setCallbackFunction('authCallback')
5353

5454
// Set the property store where authorized tokens should be persisted.
5555
.setPropertyStore(PropertiesService.getUserProperties())
56+
.setCache(CacheService.getUserCache())
5657
}
5758

5859
/**
@@ -62,13 +63,12 @@ function authCallback(request) {
6263
var service = getService();
6364
var authorized = service.handleCallback(request);
6465
if (authorized) {
65-
// Gets the authorized account ID from the scope string. Assumes the
66-
// application is configured to work with single accounts. Has the format
67-
// "harvest:{ACCOUNT_ID}".
66+
// Gets the authorized account ID from the scope string. Assumes the application is
67+
// configured to work with single accounts. Has the format "harvest:{ACCOUNT_ID}".
6868
var scope = request.parameter['scope'];
6969
var accountId = scope.split(':')[1];
70-
PropertiesService.getUserProperties()
71-
.setProperty('Harvest-Account-Id', accountId);
70+
// Save the account ID in the service's storage.
71+
service.getStorage().setValue('Harvest-Account-Id', accountId);
7272
return HtmlService.createHtmlOutput('Success!');
7373
} else {
7474
return HtmlService.createHtmlOutput('Denied.');
@@ -81,4 +81,4 @@ function authCallback(request) {
8181
function logRedirectUri() {
8282
var service = getService();
8383
Logger.log(service.getRedirectUri());
84-
}
84+
}

0 commit comments

Comments
 (0)