1+ package com .docusign .jwtconsoleapp ;
2+
3+ import org .apache .commons .cli .CommandLine ;
4+ import org .apache .commons .cli .CommandLineParser ;
5+ import org .apache .commons .cli .DefaultParser ;
6+ import org .apache .commons .cli .HelpFormatter ;
7+ import org .apache .commons .cli .Options ;
8+ import org .apache .commons .cli .ParseException ;
9+
10+ import com .docusign .esign .api .EnvelopesApi ;
11+ import com .docusign .esign .client .ApiClient ;
12+ import com .docusign .esign .client .auth .OAuth ;
13+ import com .docusign .esign .client .auth .OAuth .OAuthToken ;
14+ import com .docusign .esign .client .auth .OAuth .UserInfo ;
15+ import com .docusign .esign .client .auth .OAuth .Account ;
16+ import com .docusign .esign .client .ApiException ;
17+ import com .docusign .esign .model .Document ;
18+ import com .docusign .esign .model .CarbonCopy ;
19+ import com .docusign .esign .model .EnvelopeDefinition ;
20+ import com .docusign .esign .model .EnvelopeSummary ;
21+ import com .docusign .esign .model .Recipients ;
22+ import com .docusign .esign .model .Signer ;
23+ import com .docusign .esign .model .Tabs ;
24+ import com .docusign .esign .model .SignHere ;
25+
26+ import java .nio .file .Files ;
27+ import java .nio .file .Paths ;
28+ import java .util .*;
29+ import java .io .*;
30+ import java .awt .Desktop ;
31+ import java .net .URI ;
32+
33+ /**
34+ * Starter class for JWTConsoleApp application.
35+ */
36+
37+ public class JWTConsoleApp {
38+
39+ static String DevCenterPage = "https://developers.docusign.com/platform/auth/consent" ;
40+ /**
41+ * Application entry point.
42+ *
43+ * @param args application command line arguments
44+ */
45+ public static void main (String [] args ) throws java .io .IOException {
46+
47+ Scanner scanner = new Scanner (System . in );
48+ System .out .println ("Welcome to the JWT Code example! " );
49+ System .out .print ("Enter the signer's email address: " );
50+ String signerEmail = scanner . nextLine ();
51+ System .out .print ("Enter the signer's name: " );
52+ String signerName = scanner . nextLine ();
53+ System .out .print ("Enter the carbon copy's email address: " );
54+ String ccEmail = scanner . nextLine ();
55+ System .out .print ("Enter the carbon copy's name: " );
56+ String ccName = scanner . nextLine ();
57+
58+ // Get information fro app.config
59+ Properties prop = new Properties ();
60+ String fileName = "app.config" ;
61+ FileInputStream fis = new FileInputStream (fileName );
62+ prop .load (fis );
63+ try
64+ {
65+ // Get access token and accountId
66+ ApiClient apiClient = new ApiClient ("https://demo.docusign.net/restapi" );
67+ apiClient .setOAuthBasePath ("account-d.docusign.com" );
68+ ArrayList <String > scopes = new ArrayList <String >();
69+ scopes .add ("signature" );
70+ scopes .add ("impersonation" );
71+ byte [] privateKeyBytes = Files .readAllBytes (Paths .get (prop .getProperty ("rsaKeyFile" )));
72+ OAuthToken oAuthToken = apiClient .requestJWTUserToken (
73+ prop .getProperty ("clientId" ),
74+ prop .getProperty ("userId" ),
75+ scopes ,
76+ privateKeyBytes ,
77+ 3600 );
78+ String accessToken = oAuthToken .getAccessToken ();
79+ UserInfo userInfo = apiClient .getUserInfo (accessToken );
80+ String accountId = userInfo .getAccounts ().get (0 ).getAccountId ();
81+
82+ // Create envelopeDefinition object
83+ EnvelopeDefinition envelope = new EnvelopeDefinition ();
84+ envelope .setEmailSubject ("Please sign this document set" );
85+ envelope .setStatus ("sent" );
86+
87+ // Create tabs object
88+ SignHere signHere = new SignHere ();
89+ signHere .setDocumentId ("1" );
90+ signHere .setPageNumber ("1" );
91+ signHere .setXPosition ("191" );
92+ signHere .setYPosition ("148" );
93+ Tabs tabs = new Tabs ();
94+ tabs .setSignHereTabs (Arrays .asList (signHere ));
95+ // Set recipients
96+ Signer signer = new Signer ();
97+ signer .setEmail (signerEmail );
98+ signer .setName (signerName );
99+ signer .recipientId ("1" );
100+ signer .setTabs (tabs );
101+ CarbonCopy cc = new CarbonCopy ();
102+ cc .setEmail (ccEmail );
103+ cc .setName (ccName );
104+ cc .recipientId ("2" );
105+ Recipients recipients = new Recipients ();
106+ recipients .setSigners (Arrays .asList (signer ));
107+ recipients .setCarbonCopies (Arrays .asList (cc ));
108+ envelope .setRecipients (recipients );
109+
110+ // Add document
111+ Document document = new Document ();
112+ document .setDocumentBase64 ("VGhhbmtzIGZvciByZXZpZXdpbmcgdGhpcyEKCldlJ2xsIG1vdmUgZm9yd2FyZCBhcyBzb29uIGFzIHdlIGhlYXIgYmFjay4=" );
113+ document .setName ("doc1.txt" );
114+ document .setFileExtension ("txt" );
115+ document .setDocumentId ("1" );
116+ envelope .setDocuments (Arrays .asList (document ));
117+
118+ // Send envelope
119+ apiClient .addDefaultHeader ("Authorization" , "Bearer " + accessToken );
120+ EnvelopesApi envelopesApi = new EnvelopesApi (apiClient );
121+ EnvelopeSummary results = envelopesApi .createEnvelope (accountId , envelope );
122+ System .out .println ("Successfully sent envelope with envelopeId " + results .getEnvelopeId ());
123+ }
124+ catch (ApiException exp )
125+ {
126+ if (exp .getMessage ().contains ("consent_required" ))
127+ {
128+ try
129+ {
130+ System .out .println ("Consent required, please provide consent in browser window and then run this app again." );
131+ Desktop .getDesktop ().browse (new URI ("https://account-d.docusign.com/oauth/auth?response_type=code&scope=impersonation%20signature&client_id=" + prop .getProperty ("clientId" ) + "&redirect_uri=" + DevCenterPage ));
132+ }
133+ catch (Exception e )
134+ {
135+ System .out .print ("Error!!! " );
136+ System .out .print (e .getMessage ());
137+ }
138+ }
139+ }
140+ catch (Exception e )
141+ {
142+ System .out .print ("Error!!! " );
143+ System .out .print (e .getMessage ());
144+ }
145+ }
146+ }
0 commit comments