forked from googleworkspace/apps-script-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdobeSign.gs
101 lines (89 loc) · 3.06 KB
/
AdobeSign.gs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* This sample demonstrates how to configure the library for the Adobe Sign API.
* Instructions on how to generate OAuth keys is available here:
* https://www.adobe.io/apis/documentcloud/sign/docs/step-by-step-guide/configure-oauth.html
*/
var CLIENT_ID = '...';
var CLIENT_SECRET = '...';
var API_ACCESS_POINT_KEY = 'api_access_point';
/**
* Authorizes and makes a request to the Adobe Sign API.
*/
function run() {
var service = getService();
if (service.hasAccess()) {
// Retrieve the API access point from storage.
var apiAccessPoint = service.getStorage().getValue(API_ACCESS_POINT_KEY);
var url = apiAccessPoint + 'api/rest/v5/users/me';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
if (service.getLastError()) {
Logger.log(service.getLastError());
}
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
getService().reset();
}
/**
* Configures the service.
*/
function getService(optApiAccessPoint) {
var service = OAuth2.createService('AdobeSign')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('https://secure.echosign.com/public/oauth')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scopes.
.setScope('user_read');
// Set the token and refresh URL using the API access point passed in or
// stored in the token.
var apiAccessPoint = optApiAccessPoint ||
service.getStorage().getValue(API_ACCESS_POINT_KEY);
if (apiAccessPoint) {
service.setTokenUrl(apiAccessPoint + 'oauth/token');
service.setRefreshUrl(apiAccessPoint + 'oauth/refresh');
}
return service;
}
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
// Get the API access point specified in the URL parameters.
var apiAccessPoint = request.parameter[API_ACCESS_POINT_KEY];
var service = getService(apiAccessPoint);
var authorized = service.handleCallback(request);
if (authorized) {
// Save the API access point in the service's storage.
service.getStorage().setValue(API_ACCESS_POINT_KEY, apiAccessPoint);
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
/**
* Logs the redict URI to register in the Dropbox application settings.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
}