Docusignapi 如何在信封创建时添加附件
我正在创建一个嵌入式解决方案。在我的应用程序中,用户可以上传文件。我的模板中有一个必需的附件字段。如果用户在我的应用程序中上载了文件,我希望在创建信封时附加该文件,否则我希望docusign签名视图强制附加该文件。在任何一种情况下,我都希望它看起来与接收最终签名文档的用户相同(即:作为单独的附件)。我正在寻找如何实现这一点的XML示例。这篇文章有一个推荐的解决方案,与我想做的类似,但是没有代码:这篇文章提到了一个可能是我想要的例子,但是链接断了: 或者如果有人能给我指一下当前的位置: 我正在寻找的非工作示例:Docusignapi 如何在信封创建时添加附件,docusignapi,Docusignapi,我正在创建一个嵌入式解决方案。在我的应用程序中,用户可以上传文件。我的模板中有一个必需的附件字段。如果用户在我的应用程序中上载了文件,我希望在创建信封时附加该文件,否则我希望docusign签名视图强制附加该文件。在任何一种情况下,我都希望它看起来与接收最终签名文档的用户相同(即:作为单独的附件)。我正在寻找如何实现这一点的XML示例。这篇文章有一个推荐的解决方案,与我想做的类似,但是没有代码:这篇文章提到了一个可能是我想要的例子,但是链接断了: 或者如果有人能给我指一下当前的位置: 我正在寻
public void AddAttachment2Envelope(string envelopeID, byte[] attachment, string attachmentname)
{
string url = baseURL + "/envelopes/" + envelopeID + "/documents";
string requestBody =
"--AAA" + "\r\n" +
"Content-Type: application/xml" + "\r\n" +
"Content-Disposition: form-data" + "\r\n" +
"<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" + "\r\n" +
"<documents>" + "\r\n" +
"<document>" + "\r\n" +
"<documentId>12345</documentId>" + "\r\n" + //what is documentid here used for?
"<name>" + attachmentname + "</name>" + "\r\n" +
"<order>1</order>" + "\r\n" +
"</document>" + "\r\n" +
"</documents>" + "\r\n" +
"</envelopeDefinition>" + "\r\n" +
"--AAA" + "\r\n" +
"Content-Type: application/pdf" + "\r\n" +
"Content-Disposition: file; filename=\"String content\"; documentId=10" + "\r\n" + //what is documentid here used for?
attachment.ToString() + "\r\n" +
"--AAA";
HttpWebRequest request = initializeRequest(url, "PUT", requestBody, email, password);
request.ContentType = "multipart/form-data; boundary=AAA";
string response = getResponseBody(request);
}
public void AddAttachment2Envelope(字符串信封ID,字节[]附件,字符串attachmentname)
{
字符串url=baseURL+“/envelopes/”+envelopeID+“/documents”;
字符串请求体=
--AAA“+”\r\n+
“内容类型:应用程序/xml”+“\r\n”+
内容处置:表单数据“+”\r\n+
“”+“\r\n”+
“”+“\r\n”+
“”+“\r\n”+
“12345”+“\r\n”+//这里的documentid用于什么?
“”+附件名+“”+“\r\n”+
“1”+“\r\n”+
“”+“\r\n”+
“”+“\r\n”+
“”+“\r\n”+
--AAA“+”\r\n+
“内容类型:应用程序/pdf”+“\r\n”+
“内容处置:文件;文件名=\“字符串内容\”documentId=10”+“\r\n”+//documentId在这里用于什么?
附件.ToString()+“\r\n”+
“--AAA”;
HttpWebRequest请求=初始化请求(url,“PUT”、请求主体、电子邮件、密码);
request.ContentType=“多部分/表单数据;边界=AAA”;
字符串响应=GetResponseBy(请求);
}
我还尝试了以下方法,但没有成功:
public void AddAttachment2Envelope(string envelopeID, byte[] attachment, string attachmentname, Boolean bad)
{
string ctype = "Content-Type: application/pdf";
if (attachmentname.ToLower().EndsWith(".jpg"))
ctype = "Content-Type: image/jpeg";
else if (attachmentname.ToLower().EndsWith(".png"))
ctype = "Content-Type: image/png";
string url = baseURL + "/envelopes/" + envelopeID + "/documents/10";
string requestBody =
ctype + "\r\n" +
"Content-Disposition: file; filename=\"" + attachmentname + "\"; documentId=10" + "\r\n" +
System.Text.Encoding.Default.GetString(attachment) + "\r\n" + //System.Text.Encoding.Default.GetString(attachment) //System.Convert.ToBase64String(attachment, 0, attachment.Length)
"";
HttpWebRequest request = initializeRequest(url, "PUT", requestBody, email, password);
request.ContentType = ctype.Replace("Content-Type: ", "");
request.Headers.Add("Content-Disposition", "file; filename=\"" + attachmentname + "\"; documentId=10");
string response = getResponseBody(request);
}
public void AddAttachment2Envelope(string envelopeID, byte[] attachment, string attachmentname)
{
string ctype = "Content-Type: application/pdf";
if (attachmentname.ToLower().EndsWith(".jpg"))
ctype = "Content-Type: image/jpeg";
else if (attachmentname.ToLower().EndsWith(".png"))
ctype = "Content-Type: image/png";
string url = baseURL + "/envelopes/" + envelopeID + "/documents";
string requestBody =
"--AAA" + "\r\n" +
"Content-Type: application/xml" + "\r\n" +
"Content-Disposition: form-data" + "\r\n" +
//"<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" + "\r\n" +
//"<documents>" + "\r\n" +
"<document>" + "\r\n" +
"<documentId>10</documentId>" + "\r\n" +
"<name>" + attachmentname + "</name>" + "\r\n" +
"<order>2</order>" + "\r\n" +
//"<FileExtension>" + System.IO.Path.GetExtension(attachmentname).Replace(".", "").ToLower() + "</FileExtension>" + "\r\n" +
"</document>" + "\r\n" +
//"</documents>" + "\r\n" +
//"</envelopeDefinition>" + "\r\n" +
"--AAA" + "\r\n" +
ctype + "\r\n" +
"Content-Disposition: file; filename=\"" + attachmentname + "\"; documentId=10" + "\r\n" +
System.Convert.ToBase64String(attachment, 0, attachment.Length) + "\r\n" + //System.Text.Encoding.Default.GetString(attachment)
"--AAA--";
HttpWebRequest request = initializeRequest(url, "PUT", requestBody, email, password);
request.ContentType = "multipart/form-data; boundary=AAA";
//request.Accept = "multipart/form-data;";
string response = getResponseBody(request);
}
public void AddAttachment2Envelope(string envelopeID, byte[] attachment, string attachmentname, int bad)
{
string url = baseURL + "/envelopes/" + envelopeID + "/documents";
string requestBody =
// "<envelopeDefinition xmlns=\"http://www.docusign.com/restapi\">" + "\r\n" +
// "<documents>" + "\r\n" +
"<document>" + "\r\n" +
"<documentId>10</documentId>" + "\r\n" +
"<name>" + attachmentname + "</name>" + "\r\n" +
"<order>2</order>" + "\r\n" +
"<FileExtension>" + System.IO.Path.GetExtension(attachmentname).Replace(".", "").ToLower() + "</FileExtension>" + "\r\n" +
"<documentBase64>" + System.Convert.ToBase64String(attachment, 0, attachment.Length) + "</documentBase64>" + "\r\n" +
"</document>" + "\r\n" +
// "</documents>" + "\r\n" +
// "</envelopeDefinition>" + "\r\n" +
"";
//requestBody = requestBody.Replace("\r\n", "");
HttpWebRequest request = initializeRequest(url, "PUT", requestBody, email, password);
string response = getResponseBody(request);
}
public void AddAttachment2Envelope(字符串信封ID,字节[]附件,字符串attachmentname,布尔错误)
{
string ctype=“内容类型:应用程序/pdf”;
if(attachmentname.ToLower().EndsWith(“.jpg”))
ctype=“内容类型:图像/jpeg”;
else if(attachmentname.ToLower().EndsWith(“.png”))
ctype=“内容类型:图像/png”;
字符串url=baseURL+“/envelopes/”+envelopeID+“/documents/10”;
字符串请求体=
ctype+“\r\n”+
“内容处置:文件;文件名=\”“+attachmentname+”\“documentId=10”+“\r\n”+
System.Text.Encoding.Default.GetString(附件)+“\r\n”+//System.Text.Encoding.Default.GetString(附件)//System.Convert.ToBase64String(附件,0,附件.Length)
"";
HttpWebRequest请求=初始化请求(url,“PUT”、请求主体、电子邮件、密码);
request.ContentType=ctype.Replace(“内容类型:,”);
添加(“内容处置”、“文件;文件名=\”“+attachmentname+”\“documentId=10”);
字符串响应=GetResponseBy(请求);
}
public void AddAttachment2Envelope(字符串信封ID,字节[]附件,字符串attachmentname)
{
string ctype=“内容类型:应用程序/pdf”;
if(attachmentname.ToLower().EndsWith(“.jpg”))
ctype=“内容类型:图像/jpeg”;
else if(attachmentname.ToLower().EndsWith(“.png”))
ctype=“内容类型:图像/png”;
字符串url=baseURL+“/envelopes/”+envelopeID+“/documents”;
字符串请求体=
--AAA“+”\r\n+
“内容类型:应用程序/xml”+“\r\n”+
内容处置:表单数据“+”\r\n+
//“”+“\r\n”+
//“”+“\r\n”+
“”+“\r\n”+
“10”+“\r\n”+
“”+附件名+“”+“\r\n”+
“2”+“\r\n”+
//“+System.IO.Path.GetExtension(attachmentname)。替换(“.”,“).ToLower()+”“+”\r\n”+
“”+“\r\n”+
//“”+“\r\n”+
//“”+“\r\n”+
--AAA“+”\r\n+
ctype+“\r\n”+
“内容处置:文件;文件名=\”“+attachmentname+”\“documentId=10”+“\r\n”+
System.Convert.ToBase64String(附件,0,附件.Length)+“\r\n”+//System.Text.Encoding.Default.GetString(附件)
“--AAA——”;
HttpWebRequest请求=初始化请求(url,“PUT”、请求主体、电子邮件、密码);
request.ContentType=“多部分/表单数据;边界=AAA”;
//request.Accept=“多部分/表单数据;”;
字符串响应=GetResponseBy(请求);
}
public void AddAttachment2Envelope(字符串信封ID,字节[]附件,字符串attachmentname,int-bad)
{
字符串url=baseURL+“/envelopes/”+envelopeID+“/documents”;
字符串请求体=
//“”+“\r\n”+
//“”+“\r\n”+
“”+“\r\n”+
“10”+“\r\n”+
“”+附件名+“”+“\r\n”+
“2”+“\r\n”+
“+System.IO.Path.GetExtension(attachmentname)。替换(“.”,“).ToLower()+”“+”\r\n”+
“+System.Convert.TOBASE64字符串(附件,0,附件.Length)+”“+”\r\n”+
“”+“\r\n”+
//“+”\r\n“+
//“”+“\r\n”+
"";
//requestBody=requestBody.Replace(“\r\n”,”);
HttpWebRequest请求=初始化请求(url,“PUT”、请求主体、电子邮件、密码);
字符串响应=GetResponseBy(请求);
}
因此,请注意此处问题的措辞-如前所述,“附件”在DocuSign平台中称为“签名者附件”,只能通过单个收件人的签名者附件选项卡添加
另一方面,我们通常将“文档”称为“信封文档”,如前所述,当信封处于草稿状态时,您可以向信封中添加文档:
这是您要对/accounts/{accountId}/envelopes/{envelopeId}/documents
URL进行的PUT
调用。下面是一个完整的C代码示例(从DocuSign API演练中复制):
使用系统;
使用System.IO;
Net系统;
使用System.Xml;
使用System.Xml.Linq;
名称空间docusignaptest
{
公共类addDocumentToInvelope
{
公共静电
using System;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Linq;
namespace DocuSignAPITest
{
public class AddDocumentToEnvelope
{
public static void Main ()
{
//---------------------------------------------------------------------------------------------------
// ENTER VALUES FOR THE FOLLOWING 6 VARIABLES:
//---------------------------------------------------------------------------------------------------
string username = "***"; // your account email
string password = "***"; // your account password
string integratorKey = "***"; // your account Integrator Key (found on Preferences -> API page)
string documentName = "***"; // copy document with same name and extension into project directory (i.e. "test.pdf")
string envelopeId = "***"; // GUID of envelope we will add document to
string contentType = "application/pdf"; // default content type is PDF
//---------------------------------------------------------------------------------------------------
// additional variable declarations
string baseURL = ""; // - we will retrieve this through the Login API call
try {
//============================================================================
// STEP 1 - Login API Call (used to retrieve your baseUrl)
//============================================================================
// Endpoint for Login api call (in demo environment):
string url = "https://demo.docusign.net/restapi/v2/login_information";
// set request url, method, and headers. No body needed for login api call
HttpWebRequest request = initializeRequest( url, "GET", null, username, password, integratorKey);
// read the http response
string response = getResponseBody(request);
// parse baseUrl from response body
baseURL = parseDataFromResponse(response, "baseUrl");
//--- display results
Console.WriteLine("\nAPI Call Result: \n\n" + prettyPrintXml(response));
//============================================================================
// STEP 2 - Add document to draft envelope
//============================================================================
// append "/envelopes/{envelopeId}/documents" to baseURL
url = baseURL + "/envelopes/" + envelopeId + "/documents";
// construct the request body
string jsonBody =
"{\"documents\": [" +
"{\"documentId\": \"2\"," +
"\"name\": \"test.pdf\"," +
"\"order\": \"2\"" +
"}]" +
"}";
Console.WriteLine("\nJSON: \n\n" + jsonBody);
// set request url, method, headers. Don't set the body yet, we'll set that separelty after
// we read the document bytes and configure the rest of the multipart/form-data request
request = initializeRequest( url, "PUT", null, username, password, integratorKey);
// some extra config for this api call
configureMultiPartFormDataRequest(request, jsonBody, documentName, contentType);
// read the http response
response = getResponseBody(request);
//--- display results
Console.WriteLine("\nAPI Call Result: \n\n" + prettyPrintXml(response));
// Console.WriteLine("\nAPI Call Result: \n\n" + response);
}
catch (WebException e) {
using (WebResponse response = e.Response) {
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
Console.WriteLine(prettyPrintXml(text));
}
}
}
} // end main()
//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//***********************************************************************************************
public static HttpWebRequest initializeRequest(string url, string method, string body, string email, string password, string intKey)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
request.Method = method;
addRequestHeaders( request, email, password, intKey );
if( body != null )
addRequestBody(request, body);
return request;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void addRequestHeaders(HttpWebRequest request, string email, string password, string intKey)
{
// authentication header can be in JSON or XML format. XML used for this walkthrough:
string authenticateStr =
"<DocuSignCredentials>" +
"<Username>" + email + "</Username>" +
"<Password>" + password + "</Password>" +
"<IntegratorKey>" + intKey + "</IntegratorKey>" +
"</DocuSignCredentials>";
request.Headers.Add ("X-DocuSign-Authentication", authenticateStr);
request.Accept = "application/xml";
request.ContentType = "application/xml";
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void addRequestBody(HttpWebRequest request, string requestBody)
{
// create byte array out of request body and add to the request object
byte[] body = System.Text.Encoding.UTF8.GetBytes (requestBody);
Stream dataStream = request.GetRequestStream ();
dataStream.Write (body, 0, requestBody.Length);
dataStream.Close ();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void configureMultiPartFormDataRequest(HttpWebRequest request, string jsonBody, string docName, string contentType)
{
// overwrite the default content-type header and set a boundary marker
request.ContentType = "multipart/form-data; boundary=BOUNDARY";
// start building the multipart request body
string requestBodyStart = "\r\n\r\n--BOUNDARY\r\n" +
"Content-Type: application/json\r\n" +
"Content-Disposition: form-data\r\n" +
"\r\n" +
jsonBody + "\r\n\r\n--BOUNDARY\r\n" + // our json formatted request body
"Content-Type: " + contentType + "\r\n" +
"Content-Disposition: file; filename=\"" + docName + "\"; documentId=2\r\n" +
"\r\n";
string requestBodyEnd = "\r\n--BOUNDARY--\r\n\r\n";
// read contents of provided document into the request stream
FileStream fileStream = File.OpenRead(docName);
// write the body of the request
byte[] bodyStart = System.Text.Encoding.UTF8.GetBytes(requestBodyStart.ToString());
byte[] bodyEnd = System.Text.Encoding.UTF8.GetBytes(requestBodyEnd.ToString());
Stream dataStream = request.GetRequestStream();
dataStream.Write(bodyStart, 0, requestBodyStart.ToString().Length);
// Read the file contents and write them to the request stream. We read in blocks of 4096 bytes
byte[] buf = new byte[4096];
int len;
while ((len = fileStream.Read(buf, 0, 4096) ) > 0) {
dataStream.Write(buf, 0, len);
}
dataStream.Write(bodyEnd, 0, requestBodyEnd.ToString().Length);
dataStream.Close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static string getResponseBody(HttpWebRequest request)
{
// read the response stream into a local string
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse ();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string responseText = sr.ReadToEnd();
return responseText;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static string parseDataFromResponse(string response, string searchToken)
{
// look for "searchToken" in the response body and parse its value
using (XmlReader reader = XmlReader.Create(new StringReader(response))) {
while (reader.Read()) {
if((reader.NodeType == XmlNodeType.Element) && (reader.Name == searchToken))
return reader.ReadString();
}
}
return null;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
public static string prettyPrintXml(string xml)
{
// print nicely formatted xml
try {
XDocument doc = XDocument.Parse(xml);
return doc.ToString();
}
catch (Exception) {
return xml;
}
}
} // end class
} // end namespace