|
| 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 | +} |
0 commit comments