forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (87 loc) · 4.61 KB
/
Program.cs
File metadata and controls
94 lines (87 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using DocuSign.CodeExamples.Authentication;
using DocuSign.eSign.Client;
using static DocuSign.eSign.Client.Auth.OAuth;
using static DocuSign.eSign.Client.Auth.OAuth.UserInfo;
using eSignature.Examples;
using System;
using System.Diagnostics;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.IO;
namespace DocuSign.CodeExamples.JWT_Console
{
class Program
{
static readonly string DevCenterPage = "https://developers.docusign.com/platform/auth/consent";
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
OAuthToken accessToken = null;
try
{
accessToken = JWTAuth.AuthenticateWithJWT("ESignature", ConfigurationManager.AppSettings["ClientId"], ConfigurationManager.AppSettings["ImpersonatedUserId"],
ConfigurationManager.AppSettings["AuthServer"], ConfigurationManager.AppSettings["PrivateKeyFile"]);
}
catch (ApiException apiExp)
{
// Consent for impersonation must be obtained to use JWT Grant
if (apiExp.Message.Contains("consent_required"))
{
// Caret needed for escaping & in windows URL
string caret = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
caret = "^";
}
// build a URL to provide consent for this Integration Key and this userId
string url = "https://" + ConfigurationManager.AppSettings["AuthServer"] + "/oauth/auth?response_type=code" + caret + "&scope=impersonation%20signature" + caret +
"&client_id=" + ConfigurationManager.AppSettings["ClientId"] + caret + "&redirect_uri=" + DevCenterPage;
Console.WriteLine($"Consent is required - launching browser (URL is {url})");
// Start new browser window for login and consent to this app by DocuSign user
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = false });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unable to send envelope; Exiting. Please rerun the console app once consent was provided");
Console.ForegroundColor = ConsoleColor.White;
Environment.Exit(-1);
}
}
var apiClient = new ApiClient();
apiClient.SetOAuthBasePath(ConfigurationManager.AppSettings["AuthServer"]);
UserInfo userInfo = apiClient.GetUserInfo(accessToken.access_token);
Account acct = userInfo.Accounts.FirstOrDefault();
Console.WriteLine("Welcome to the JWT Code example! ");
Console.Write("Enter the signer's email address: ");
string signerEmail = Console.ReadLine();
Console.Write("Enter the signer's name: ");
string signerName = Console.ReadLine();
Console.Write("Enter the carbon copy's email address: ");
string ccEmail = Console.ReadLine();
Console.Write("Enter the carbon copy's name: ");
string ccName = Console.ReadLine();
string docDocx = Path.Combine(@"..", "..", "..", "..", "launcher-csharp", "World_Wide_Corp_salary.docx");
string docPdf = Path.Combine(@"..", "..", "..", "..", "launcher-csharp", "World_Wide_Corp_lorem.pdf");
Console.WriteLine("");
string envelopeId = SigningViaEmail.SendEnvelopeViaEmail(signerEmail, signerName, ccEmail, ccName, accessToken.access_token, acct.BaseUri + "/restapi", acct.AccountId, docDocx, docPdf, "sent");
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Successfully sent envelope with envelopeId {envelopeId}");
Console.WriteLine("");
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.White;
Environment.Exit(0);
}
}
}