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