Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var DOCUSIGN_URL='xxx';
  2. var DOCUSIGN_USER='xxx';
  3. var DOCUSIGN_PASS='xxx';
  4. var DOCUSIGN_KEY='xxx';
  5.  
  6. (function () {
  7.     var apiClient = new docusign.ApiClient();
  8.     apiClient.setBasePath(DOCUSIGN_URL);
  9.     apiClient.addDefaultHeader('X-DocuSign-Authentication', JSON.stringify({
  10.         Username: DOCUSIGN_USER,
  11.         Password: DOCUSIGN_PASS,
  12.         IntegratorKey: DOCUSIGN_KEY
  13.     }));
  14.     docusign.Configuration.default.setDefaultApiClient(apiClient);
  15.  
  16.     var loginData;
  17.  
  18.     async.waterfall([
  19.         // Make sure login works.
  20.         function (next) {
  21.             var authenticationApi = new docusign.AuthenticationApi();
  22.             var loginOps = new authenticationApi.LoginOptions();
  23.             // loginOps.setApiPassword('true');
  24.             loginOps.setIncludeAccountIdGuid('true');
  25.  
  26.             authenticationApi.login(loginOps, function (err, result, response) {
  27.                 // console.log(response.request.req._headers);
  28.                 // console.log(response.body);
  29.                 if (err) {
  30.                     return next(err);
  31.                 }
  32.  
  33.                 if (result) {
  34.                     loginData = result.loginAccounts[0];
  35.                     console.log(result);
  36.                     return next();
  37.                 }
  38.  
  39.                 next(new Error('Protocol Error'));
  40.             });
  41.         },
  42.         // List templates
  43.         function (next) {
  44.             var templatesApi = new docusign.TemplatesApi();
  45.             templatesApi.listTemplates(loginData.accountId, function (err, result, response) {
  46.                 if (err) {
  47.                     return next(err);
  48.                 }
  49.  
  50.                 if (result) {
  51.                     console.log(result.envelopeTemplates);
  52.                     return next(null, result.getEnvelopeTemplates());
  53.                 }
  54.  
  55.                 next(new Error('Protocol Error'));
  56.             });
  57.         },
  58.         // Get Document template
  59.         function (templates, next) {
  60.             var templatesApi = new docusign.TemplatesApi();
  61.             if (templates.length == 0) {
  62.                 next(new Error('No templates found'));
  63.             }
  64.             var documentTemplate = templates[0];
  65.  
  66.             if (!documentTemplate) {
  67.                 return next(new Error('Template error'));
  68.             }
  69.  
  70.             console.log(loginData.accountId, documentTemplate.templateId);
  71.             templatesApi.get(loginData.accountId, documentTemplate.templateId, function (err, result, response) {
  72.                 if (err) {
  73.                     return next(err);
  74.                 }
  75.  
  76.                 if (result) {
  77.                     console.log(result);
  78.                     // return next(null, result.getEnvelopeTemplates());
  79.                 }
  80.  
  81.                 // next(new Error('Protocol Error'));
  82.             });
  83.  
  84.         }
  85.     ], function (err, result) {
  86.         if (err) {
  87.             console.error('Failure:', err.stack);
  88.         } else {
  89.             console.log('Success');
  90.         }
  91.     })
  92. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement