forked from docusign/code-examples-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eg010SendBinaryDocs.ps1
172 lines (152 loc) · 5.25 KB
/
eg010SendBinaryDocs.ps1
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function Add-OemContent {
param(
$destination,
$content
)
Add-Content -Path $destination -Value $content -Encoding oem -NoNewline
}
# Configuration
# 1. Get required variables from .\config\settings.json:
$variables = Get-Content .\config\settings.json -Raw | ConvertFrom-Json
$CC_EMAIL = $variables.CC_EMAIL
$CC_NAME = $variables.CC_NAME
$SIGNER_EMAIL = $variables.SIGNER_EMAIL
$SIGNER_NAME = $variables.SIGNER_NAME
# Step 2. Obtain your OAuth access token
$accessToken = Get-Content ".\config\ds_access_token.txt"
# Obtain your accountId from demo.docusign.net -- the account id is shown in
# the drop down on the upper right corner of the screen by your picture or
# the default picture.
$accountId = Get-Content ".\config\API_ACCOUNT_ID"
# ***DS.snippet.0.start
# Step 3. Construct the request body
# document 1 (html) has tag **signature_1**
# document 2 (docx) has tag /sn1/
# document 3 (pdf) has tag /sn1/
#
# The envelope has two recipients.
# recipient 1 - signer
# recipient 2 - cc
# The envelope will be sent first to the signer.
# After it is signed, a copy is sent to the cc person.
$apiUri = "https://demo.docusign.net/restapi"
# temp files
$requestData = New-TemporaryFile
$response = New-TemporaryFile
$doc1 = Get-Item ".\demo_documents\doc_1.html"
$doc2 = Get-Item ".\demo_documents\World_Wide_Corp_Battle_Plan_Trafalgar.docx"
$doc3 = Get-Item ".\demo_documents\World_Wide_Corp_lorem.pdf"
Write-Output "Sending the envelope request to DocuSign..."
Write-Output "The envelope has three documents. Processing time will be about 15 seconds."
Write-Output "Results:"
$json = @{
emailSubject = "Please sign this document set";
documents = @(@{
name = "Order acknowledgement";
fileExtension = "html";
documentId = "1";
}; @{
name = "Battle Plan";
fileExtension = "docx";
documentId = "2";
}; @{
name = "Lorem Ipsum";
fileExtension = "pdf";
documentId = "3";
});
recipients = @{
signers = @(@{
email = $SIGNER_EMAIL;
name = $SIGNER_NAME;
recipientId = "1";
routingOrder = "1";
tabs = @{
signHereTabs = @(@{
anchorString = "**signature_1**";
anchorYOffset = "10";
anchorUnits = "pixels";
anchorXOffset = "20";
}; @{
anchorString = "/sn1/";
anchorYOffset = "10";
anchorUnits = "pixels";
anchorXOffset = "20";
})
}
});
carbonCopies = @(@{
email = $CC_EMAIL;
name = $CC_NAME;
routingOrder = 2;
recipientId = 2;
})
};
status = "sent"
} | ConvertTo-Json -Depth 32 -Compress;
$CRLF = "`r`n"
$boundary = "multipartboundary_multipartboundary"
Add-OemContent $requestData "--$boundary"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Type: application/json"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Disposition: form-data"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData $json
Add-OemContent $requestData "${CRLF}"
# Next add the documents. Each document has its own mime type,
# filename, and documentId. The filename and documentId must match
# the document's info in the JSON.
Add-OemContent $requestData "--$boundary"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Type: text/html"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Disposition: file; filename=`"Order acknowledgement`";documentid=1"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData (Get-Content $doc1 -Encoding oem)
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "--$boundary"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Disposition: file; filename=`"Battle Plan`";documentid=2"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData (Get-Content $doc2 -Encoding oem -Raw)
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "--$boundary"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Type: application/pdf"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "Content-Disposition: file; filename=`"Lorem Ipsum`";documentid=3"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData "${CRLF}"
Add-OemContent $requestData (Get-Content $doc3 -Encoding oem -Raw)
Add-OemContent $requestData "${CRLF}"
# Add closing boundary
Add-OemContent $requestData "--$boundary--"
Add-OemContent $requestData "${CRLF}"
# Send request
try {
# Step 4. Call the eSignature REST API
Invoke-RestMethod `
-Uri "${apiUri}/v2.1/accounts/${accountId}/envelopes" `
-Method 'POST' `
-Headers @{
'Authorization' = "Bearer $accessToken";
'Content-Type' = "multipart/form-data; boundary=${boundary}";
} `
-InFile (Resolve-Path $requestData).Path `
-OutFile $response
Write-Output "Response: $(Get-Content -Raw $response)"
}
catch {
Write-Error $_
}
# ***DS.snippet.0.end
Get-Content $response
# cleanup
Remove-Item $requestData
Remove-Item $response
Write-Output "Done."