Skip to content

Commit d8172fc

Browse files
authored
Merge pull request #43 from docusign/docusign-oauth
Added support for DocuSign OAuth
2 parents d498e86 + 955050f commit d8172fc

File tree

687 files changed

+1805
-1348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

687 files changed

+1805
-1348
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ coverage
55
auth-info.json
66
config.json
77
.idea/
8-
npm-debug.log
8+
npm-debug.log
9+
*.class
10+
target
11+
.classpath
12+
.settings
13+
.project

README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,114 @@ Or you can manually download and add the following JARs to your project:
8686
Usage
8787
=====
8888

89-
To send a signature request from a Template:
89+
To send a signature request from a Template using 3-legged OAuth:
90+
91+
```java
92+
import com.docusign.esign.api.*;
93+
import com.docusign.esign.client.*;
94+
import com.docusign.esign.model.*;
95+
96+
import java.util.List;
97+
98+
public class DocuSignExample {
99+
public static void main(String[] args) {
100+
String RedirectURI = "[REDIRECT_URI]";
101+
String ClientSecret = "[CLIENT_SECRET]";
102+
String integratorKey = "[INTEGRATOR_KEY]";
103+
String BaseUrl = "https://demo.docusign.net/restapi";
104+
String OAuthBaseUrl = "https://account-d.docusign.com";
105+
106+
// initialize client for desired environment and add X-DocuSign-Authentication header
107+
ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
108+
109+
ApiClient apiClient = new ApiClient(OAuthBaseUrl, "docusignAccessCode", IntegratorKey, ClientSecret);
110+
apiClient.setBasePath(BaseUrl);
111+
// make sure to pass the redirect uri
112+
apiClient.configureAuthorizationFlow(IntegratorKey, ClientSecret, RedirectURI);
113+
Configuration.setDefaultApiClient(apiClient);
114+
try
115+
{
116+
// get DocuSign OAuth authorization url
117+
String oauthLoginUrl = apiClient.getAuthorizationUri();
118+
// open DocuSign OAuth login in the browser
119+
System.out.println(oauthLoginUrl);
120+
Desktop.getDesktop().browse(URI.create(oauthLoginUrl));
121+
// IMPORTANT: after the login, DocuSign will send back a fresh
122+
// authorization code as a query param of the redirect URI.
123+
// You should set up a route that handles the redirect call to get
124+
// that code and pass it to token endpoint as shown in the next
125+
// lines:
126+
String code = "<once_you_get_the_oauth_code_put_it_here>";
127+
// assign it to the token endpoint
128+
apiClient.getTokenEndPoint().setCode(code);
129+
// optionally register to get notified when a new token arrives
130+
apiClient.registerAccessTokenListener(new AccessTokenListener() {
131+
@Override
132+
public void notify(BasicOAuthToken token) {
133+
System.out.println("Got a fresh token: " + token.getAccessToken());
134+
}
135+
});
136+
// ask to exchange the auth code with an access code
137+
apiClient.updateAccessToken();
138+
139+
/////////////////////////////////////////////////////////////////////////////////////////////////////////
140+
// STEP 1: AUTHENTICATE TO RETRIEVE ACCOUNTID & BASEURL
141+
/////////////////////////////////////////////////////////////////////////////////////////////////////////
142+
143+
AuthenticationApi authApi = new AuthenticationApi();
144+
LoginInformation loginInfo = authApi.login();
145+
146+
// parse first account ID (user might belong to multiple accounts) and baseUrl
147+
String accountId = loginInfo.getLoginAccounts().get(0).getAccountId();
148+
String baseUrl = loginInfo.getLoginAccounts().get(0).getBaseUrl();
149+
String[] accountDomain = baseUrl.split("/v2");
150+
151+
// below code required for production, no effect in demo (same domain)
152+
apiClient.setBasePath(accountDomain[0]);
153+
Configuration.setDefaultApiClient(apiClient);
154+
155+
/////////////////////////////////////////////////////////////////////////////////////////////////////////
156+
// *** STEP 2: CREATE ENVELOPE FROM TEMPLATE
157+
/////////////////////////////////////////////////////////////////////////////////////////////////////////
158+
159+
// create a new envelope to manage the signature request
160+
EnvelopeDefinition envDef = new EnvelopeDefinition();
161+
envDef.setEmailSubject("DocuSign Java SDK - Sample Signature Request");
162+
163+
// assign template information including ID and role(s)
164+
envDef.setTemplateId("[TEMPLATE_ID]");
165+
166+
// create a template role with a valid templateId and roleName and assign signer info
167+
TemplateRole tRole = new TemplateRole();
168+
tRole.setRoleName("[ROLE_NAME]");
169+
tRole.setName("[SIGNER_NAME]");
170+
tRole.setEmail("[SIGNER_EMAIL]");
171+
172+
// create a list of template roles and add our newly created role
173+
java.util.List<TemplateRole> templateRolesList = new java.util.ArrayList<TemplateRole>();
174+
templateRolesList.add(tRole);
175+
176+
// assign template role(s) to the envelope
177+
envDef.setTemplateRoles(templateRolesList);
178+
179+
// send the envelope by setting |status| to "sent". To save as a draft set to "created"
180+
envDef.setStatus("sent");
181+
182+
// instantiate a new EnvelopesApi object
183+
EnvelopesApi envelopesApi = new EnvelopesApi();
184+
185+
// call the createEnvelope() API
186+
EnvelopeSummary envelopeSummary = envelopesApi.createEnvelope(accountId, envDef);
187+
}
188+
catch (com.docusign.esign.client.ApiException ex)
189+
{
190+
System.out.println("Exception: " + ex);
191+
}
192+
}
193+
}
194+
```
195+
196+
To send a signature request from a Template using username/password:
90197

91198
```java
92199
import com.docusign.esign.api.*;

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@
234234
<artifactId>joda-time</artifactId>
235235
<version>${jodatime-version}</version>
236236
</dependency>
237+
<dependency>
238+
<groupId>org.apache.oltu.oauth2</groupId>
239+
<artifactId>org.apache.oltu.oauth2.client</artifactId>
240+
<version>${oltu-version}</version>
241+
</dependency>
237242

238243
<!-- Base64 encoding that works in both JVM and Android -->
239244
<dependency>
@@ -258,5 +263,6 @@
258263
<jodatime-version>2.9.3</jodatime-version>
259264
<maven-plugin-version>1.0.0</maven-plugin-version>
260265
<junit-version>4.12</junit-version>
266+
<oltu-version>1.0.2</oltu-version>
261267
</properties>
262268
</project>

0 commit comments

Comments
 (0)