|
| 1 | + |
| 2 | + |
| 3 | + /** |
| 4 | + * Authorizes and makes a request to the Docusign API. |
| 5 | + */ |
| 6 | +function rundocusign() { |
| 7 | + var payload = |
| 8 | + { |
| 9 | + "emailSubject": "EMAIL-SUBJECT", |
| 10 | + "status": "sent", |
| 11 | + "emailBlurb": "EMAIL-CONTENT", |
| 12 | + "templateId": "TEMPLATE-ID-TO-BE-USED", |
| 13 | + "templateRoles": [ |
| 14 | + { |
| 15 | + |
| 16 | + "name": "Joe Blogger", |
| 17 | + "roleName": "role1" |
| 18 | + } |
| 19 | + ] |
| 20 | +} |
| 21 | + var service = getService(); |
| 22 | + if (service.hasAccess()) { |
| 23 | + var url = 'https://demo.docusign.net/restapi/v2/accounts/[INSERT-ACCOUNT-ID-HERE]/envelopes'; |
| 24 | + var response = UrlFetchApp.fetch(url, { |
| 25 | + headers: { |
| 26 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 27 | + |
| 28 | + }, |
| 29 | + method: 'post', |
| 30 | + contentType: 'application/json', |
| 31 | + grant_type: 'authorization_code', |
| 32 | + payload : JSON.stringify(payload) |
| 33 | + |
| 34 | + }); |
| 35 | + var result = response.getContentText(); |
| 36 | + Logger.log(result, null, 1); |
| 37 | + } else { |
| 38 | + |
| 39 | + |
| 40 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 41 | + Logger.log('Open the following URL and re-run the script: %s', |
| 42 | + authorizationUrl); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Reset the authorization state, so that it can be re-tested. |
| 48 | + */ |
| 49 | +function reset() { |
| 50 | + getService().reset(); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Configures the service. |
| 55 | + */ |
| 56 | +function getService() { |
| 57 | + return OAuth2.createService("Docusign") |
| 58 | + // Set the endpoint URLs. |
| 59 | + .setAuthorizationBaseUrl('https://account-d.docusign.com/oauth/auth') |
| 60 | + .setTokenUrl('https://account-d.docusign.com/oauth/token') |
| 61 | + |
| 62 | + // Set the client ID and secret. |
| 63 | + .setClientId("...") |
| 64 | + .setClientSecret("...") |
| 65 | + |
| 66 | + // Set the name of the callback function that should be invoked to |
| 67 | + // complete the OAuth flow. |
| 68 | + .setCallbackFunction('usercallback') |
| 69 | + |
| 70 | + // Set the property store where authorized tokens should be persisted. |
| 71 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 72 | + .setScope('openid') |
| 73 | + |
| 74 | + // Set the response type to code (required). |
| 75 | + |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Handles the OAuth callback. |
| 81 | + */ |
| 82 | +function authCallback(request) { |
| 83 | + var service = getService(); |
| 84 | + var authorized = service.handleCallback(request); |
| 85 | + if (authorized) { |
| 86 | + return HtmlService.createHtmlOutput('Success!'); |
| 87 | + } else { |
| 88 | + return HtmlService.createHtmlOutput('Denied.'); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Logs the redict URI to register in the Dropbox application settings. |
| 94 | + */ |
| 95 | +function logRedirectUri() { |
| 96 | + Logger.log(OAuth2.getRedirectUri()); |
| 97 | +} |
0 commit comments