|
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); |
| 1 | +/* |
| 2 | + * This sample demonstrates how to configure the library for the DocuSign API. |
| 3 | + * Instructions on how to generate OAuth credentuals is available here: |
| 4 | + * https://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-code-grant |
| 5 | + */ |
| 6 | + |
| 7 | +var CLIENT_ID = '...'; |
| 8 | +var CLIENT_SECRET = '...'; |
| 9 | + |
| 10 | +// To connect to developer sandbox accounts, use the host |
| 11 | +// "account-d.docusign.com". For production accounts, use |
| 12 | +// "account.docusign.com". |
| 13 | +var OAUTH_HOST = 'account-d.docusign.com'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Authorizes and makes a request to the Docusign API. |
| 17 | + */ |
| 18 | +function run() { |
| 19 | + var service = getService(); |
| 20 | + if (service.hasAccess()) { |
| 21 | + // Retrieve the account ID and base URI from storage. |
| 22 | + var storage = service.getStorage(); |
| 23 | + var accountId = storage.getValue('account_id'); |
| 24 | + var baseUri = storage.getValue('base_uri'); |
| 25 | + |
| 26 | + // Make a request to retrieve the account information. |
| 27 | + var url = baseUri + '/restapi/v2.1/accounts/' + accountId; |
| 28 | + var response = UrlFetchApp.fetch(url, { |
| 29 | + headers: { |
| 30 | + Authorization: 'Bearer ' + service.getAccessToken() |
35 | 31 | } |
| 32 | + }); |
| 33 | + var result = JSON.parse(response.getContentText()); |
| 34 | + Logger.log(JSON.stringify(result, null, 2)); |
| 35 | + } else { |
| 36 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 37 | + Logger.log('Open the following URL and re-run the script: %s', |
| 38 | + authorizationUrl); |
36 | 39 | } |
| 40 | +} |
37 | 41 |
|
38 | | - /** |
39 | | - * Reset the authorization state, so that it can be re-tested. |
40 | | - */ |
41 | | - function reset() { |
42 | | - getService().reset(); |
43 | | - } |
| 42 | +/** |
| 43 | + * Reset the authorization state, so that it can be re-tested. |
| 44 | + */ |
| 45 | +function reset() { |
| 46 | + getService().reset(); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Configures the service. |
| 51 | + */ |
| 52 | +function getService() { |
| 53 | + return OAuth2.createService('DocuSign') |
| 54 | + // Set the endpoint URLs. |
| 55 | + .setAuthorizationBaseUrl('https://' + OAUTH_HOST + '/oauth/auth') |
| 56 | + .setTokenUrl('https://' + OAUTH_HOST + '/oauth/token') |
| 57 | + |
| 58 | + // Set the client ID and secret. |
| 59 | + .setClientId(CLIENT_ID) |
| 60 | + .setClientSecret(CLIENT_SECRET) |
| 61 | + |
| 62 | + // Set the name of the callback function that should be invoked to |
| 63 | + // complete the OAuth flow. |
| 64 | + .setCallbackFunction('authCallback') |
44 | 65 |
|
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.'); |
| 66 | + // Set the property store where authorized tokens should be persisted. |
| 67 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 68 | + |
| 69 | + // Set the scope. The "signature" scope is used for all endpoints in the |
| 70 | + // eSignature REST API. |
| 71 | + .setScope('signature') |
| 72 | + |
| 73 | + // Set the "Authorization" header when requesting tokens, as required by the |
| 74 | + // API. |
| 75 | + .setTokenHeaders({ |
| 76 | + 'Authorization': 'Basic ' + |
| 77 | + Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET) |
| 78 | + }); |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | + * Handles the OAuth callback. |
| 83 | + */ |
| 84 | +function authCallback(request) { |
| 85 | + var service = getService(); |
| 86 | + var authorized = service.handleCallback(request); |
| 87 | + if (authorized) { |
| 88 | + // Get the user info to determine the ase URI and account ID needed for |
| 89 | + // future requests. |
| 90 | + var url = 'https://' + OAUTH_HOST + '/oauth/userinfo'; |
| 91 | + var response = UrlFetchApp.fetch(url, { |
| 92 | + headers: { |
| 93 | + Authorization: 'Bearer ' + service.getAccessToken() |
76 | 94 | } |
77 | | - } |
| 95 | + }); |
| 96 | + var result = JSON.parse(response.getContentText()); |
78 | 97 |
|
79 | | - /** |
80 | | - * Logs the redict URI to register in the Dropbox application settings. |
81 | | - */ |
82 | | - function logRedirectUri() { |
83 | | - Logger.log(OAuth2.getRedirectUri()); |
| 98 | + // Find the default account. |
| 99 | + var account = result.accounts.filter(function(account) { |
| 100 | + return account.is_default; |
| 101 | + })[0]; |
| 102 | + |
| 103 | + // Store the base URI and account ID for later. |
| 104 | + var storage = service.getStorage(); |
| 105 | + storage.setValue('account_id', account.account_id); |
| 106 | + storage.setValue('base_uri', account.base_uri); |
| 107 | + |
| 108 | + return HtmlService.createHtmlOutput('Success!'); |
| 109 | + } else { |
| 110 | + return HtmlService.createHtmlOutput('Denied.'); |
84 | 111 | } |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Logs the redict URI to register in the Dropbox application settings. |
| 116 | + */ |
| 117 | +function logRedirectUri() { |
| 118 | + Logger.log(OAuth2.getRedirectUri()); |
| 119 | +} |
0 commit comments