我试图使用 DocuSign REST API 创建包含多个文档的信封,我创建了 C#控制台应用程序,并以 JSON 格式在请求中编写了信封参数。我收到错误代码“ ENVELOPE IS INCOMPLETE”,我试图将我的请求与 REST API 文档指南中的请求进行比较,但我看不到我所缺少的内容。这是我的示例代码:
[已编辑]
public class RequestSignature
{
// Enter your info here:
static string email = "email";
static string password = "password";
static string integratorKey = "key";
public static void Main()
{
string url = "https://demo.docusign.net/restapi/v2/login_information";
string baseURL = ""; // we will retrieve this
string accountId = ""; // will retrieve
var objectCredentials = new { Username = email, Password = password, IntegratorKey = integratorKey };
string jSONCredentialsString = JsonConvert.SerializeObject(objectCredentials);
//
// STEP 1 - Login
//
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// request.Headers.Add("X-DocuSign-Authentication", authenticateStr);
request.Headers.Add("X-DocuSign-Authentication", jSONCredentialsString);
request.Accept = "application/json";
request.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string responseText = sr.ReadToEnd();
// close stream reader
sr.Close();
JsonTextReader reader = new JsonTextReader(new StringReader(responseText));
JObject jObject = JObject.Parse(responseText);
// get the first User Account data
JToken jUserAccount = jObject["loginAccounts"].First;
// read values from JSON
accountId = (string)jUserAccount["accountId"];
baseURL = (string)jUserAccount["baseUrl"];
//
// STEP 2 - Send Envelope with Information
//
// construct an outgoing JSON request body that create the envelope
string formDataBoundary = String.Format("{0:N}", Guid.NewGuid());
StringBuilder requestBody = new StringBuilder();
string header = string.Format("--{0}\r\nContent-Type: application/json\r\nContent-Disposition: form-data\r\n\r\n", formDataBoundary);
// Documents list to send in the envelope
List<Document> envelopeDocuments = new List<Document>();
Document currentDocument = new Document(1, "ABC.pdf", "C:/Documents/ABC.pdf");
envelopeDocuments.Add(currentDocument);
DocuSignDocument[] documentsArray = (from doc in envelopeDocuments
select new DocuSignDocument()
{
documentId = doc.DocumentID.ToString(),
name = doc.Name
}).ToArray();
//currentDocument = new Document(2, "ABC.pdf", "D:/Documents/ABC.pdf");
//envelopeDocuments.Add(currentDocument);
// creaqte recipients
Recipient firstRecipient = new Recipient()
{
email = "email",
name = "name",
recipientId = 1.ToString(),
routingOrder = 1.ToString(),
tabs = new Tabs()
{
signHereTabs = new List<Tab>()
{ new Tab()
{
documentId = 1.ToString(),
pageNumber = 1.ToString(),
//recipientId = 1.ToString(),
xPosition = 100.ToString(),
yPosition = 100.ToString()
}
}
}
};
List<Recipient> recipients = new List<Recipient>();
recipients.Add(firstRecipient);
// api json attributes setting by developer
// setting attributes for the envelope request
var envelopeAttributes = new
{
//allowReassign = false,
emailBlurb = "EMAIL BODY HERE OK OK",
emailSubject = "EMAIL SUBJECT HERE IS MANDATORY",
// enableWetSign = false,
// messageLock = true,
// notification attributes
/*notifications = new
{
useAccountDefaults = true,
// reminder configuration attributes
reminders = new object[]
{
new
{
reminderEnabled = true,
reminderDelay = 3,
reminderFrequency = 3
}
},
// end reminder configuration attributes
// expiration configuration attributes
expirations = new object[]
{
new
{
expirationEnabled = true,
expirationAfter = 30,
expirationWarn = 5
}
}
}, */
// end notification attributes
status = "sent",
// start documents section
documents = documentsArray,
recipients = new
{
signers = recipients
}
};
// append "/envelopes" to baseURL and use in the request
request = (HttpWebRequest)WebRequest.Create(baseURL + "/envelopes");
request.Headers.Add("X-DocuSign-Authentication", jSONCredentialsString);
request.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
request.Accept = "application/json";
request.Method = "POST";
request.KeepAlive = true;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
string requestBodyStartStr = header;
requestBodyStartStr += JsonConvert.SerializeObject(envelopeAttributes);
requestBodyStartStr += "\r\n--" + formDataBoundary + "\r\n";
// Write the body of the request
byte[] bodyStart = System.Text.Encoding.UTF8.GetBytes(requestBodyStartStr);
MemoryStream streamBufferData = new MemoryStream();
streamBufferData.Write(bodyStart, 0, bodyStart.Length);
// Read the file contents and write them to the request stream
byte[] buf = new byte[4096];
int length;
FileStream fileStream;
string mixedHeaderBoundary = String.Format("{0:N}", Guid.NewGuid());
// add multipart mixed header
string mixedHeader = "Content-Disposition: form-data\r\n";
mixedHeader += "Content-Type: multipart/mixed; boundary=" + mixedHeaderBoundary + "\r\n\r\n";
byte[] bodyMixedHeader = System.Text.Encoding.UTF8.GetBytes(mixedHeader);
streamBufferData.Write(bodyMixedHeader, 0, bodyMixedHeader.Length);
foreach (Document document in envelopeDocuments)
{
fileStream = null;
// load file from location
fileStream = File.OpenRead(document.PathName);
// write header of pdf
string headerOfDocumentStr = "--" + mixedHeaderBoundary + "\r\n" +
"Content-Type: application/pdf\r\n" +
"Content-Disposition: file; filename=\"" + document.Name + "\";documentId=\"" + document.DocumentID + "\"\r\n\r\n";
byte[] headerDocBytes = System.Text.Encoding.UTF8.GetBytes(headerOfDocumentStr);
streamBufferData.Write(headerDocBytes, 0, headerDocBytes.Length);
length = 0;
while ((length = fileStream.Read(buf, 0, 4096)) > 0)
{
streamBufferData.Write(buf, 0, length);
}
fileStream.Close();
//byte[] bottomMixedBoundaryForFDocument = System.Text.Encoding.UTF8.GetBytes("\r\n--" + mixedHeaderBoundary + "\r\n");
//streamBufferData.Write(bottomMixedBoundaryForFDocument, 0, bottomMixedBoundaryForFDocument.Length);
}
string requestBodyEndStr = "--" + mixedHeaderBoundary + "--\r\n";
byte[] requestBodyEndBytes = System.Text.Encoding.UTF8.GetBytes(requestBodyEndStr);
streamBufferData.Write(requestBodyEndBytes, 0, requestBodyEndBytes.Length);
// write end boundary
requestBodyEndStr = "--" + formDataBoundary + "--";
requestBodyEndBytes = System.Text.Encoding.UTF8.GetBytes(requestBodyEndStr);
streamBufferData.Write(requestBodyEndBytes, 0, requestBodyEndBytes.Length);
// pass temporary buffer data to WebRequestStream
request.ContentLength = streamBufferData.Length;
Stream dataStream = request.GetRequestStream();
byte[] byteArrayToSend = new byte[streamBufferData.Length];
streamBufferData.Seek(0, SeekOrigin.Begin);
streamBufferData.Read(byteArrayToSend, 0, (int)streamBufferData.Length);
dataStream.Write(byteArrayToSend, 0, (int)streamBufferData.Length);
streamBufferData.Close();
// read the response
webResponse = (HttpWebResponse)request.GetResponse();
responseText = "";
sr = new StreamReader(webResponse.GetResponseStream());
responseText = sr.ReadToEnd();
// display results
Console.WriteLine("Response of Action Create Envelope with Two Documents --> \r\n " + responseText);
Console.ReadLine();
}
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(text);
}
}
Console.ReadLine();
}
}
}
//转换为 JSON 的对象中使用的其他类
public class Tab
{
public int documentId { get; set; }
public int pageNumber { get; set; }
public int recipientId { get; set; }
public int xPosition { get; set; }
public int yPosition { get; set; }
public string name { get; set; }
public string tabLabel { get; set; }
}
public class Tabs
{
public List<Tab> signHereTabs { get; set; }
}
public class Recipient
{
public string email { get; set; }
public string name { get; set; }
// public int recipientId { get; set; }
public int routingOrder { get; set; }
public Tabs tabs { get; set; }
}
[已编辑]
//这是提琴手的请求正文
POST https://demo.docusign.net/restapi/v2/accounts/295724/envelopes HTTP/1.1 X-DocuSign-Authentication:{“用户名”:“电子邮件”,“密码”:“用户名”,“ IntegratorKey”:“密钥”} Content-Type:multipart/form-data; boundary=c17efb7771a64f688508187fee57c398 接受:application/json 主持人:demo.docusign.net Content-Length:147201 预期:100-continue
--c17efb7771a64f688508187fee57c398 Content-Type:application/json Content-Disposition:form-data
{“ emailBlurb”:“电子邮件主体在这里 OK OK”,“ emailSubject”:“电子邮件主题在这里是必填项”,“状态”:“已发送”,“文档”:[9],“收件人”:{“签名者”: [{“ email”:“ dn@brenock.com”,“ name”:“ Dubhe”,“ recipientId”:“ 1”,“ routingOrder”:“ 1”,“ tabs”:{“ signHereTabs”:[10]}}]} } --c17efb7771a64f688508187fee57c398 Content-Disposition:form-data Content-Type:multipart/mixed; boundary=b670ec35bd824dff8c0eefe62035e0b2
--b670ec35bd824dff8c0eefe62035e0b2 Content-Type:application/pdf Content-Disposition:文件; filename =“ ABC.pdf”; documentId=1
--b670ec35bd824dff8c0eefe62035e0b2-- --c17efb7771a64f688508187fee57c398--
1 回答
我相信问题在于您发送的 JSON。您使用的是有效的 JSON 格式,但是recipientId的值已删除或未设置。您有这个用于接收者 ID:
要解决此问题,请尝试将此设置为
或您想为其设置的任何值,因为它是用户可配置的。例如,您可以根据需要将其设置为“ 4321”。
此外,DocuSign 允许您根据需要设置分配接收者 ID,但是,如果您根本不指定 receiveId 属性,DocuSign 应该为接收者生成一个 GUID。因此,我认为解决此问题的另一种方法是根本不包括
recipientId
属性,例如:我相信任何一种解决方案都可以解决您的问题。
[编辑]
我在 Fiddler 输出中看到,您添加了一些边界之间缺少 CRLF 字符的功能。那也可能导致您的问题。
例如,如果要发送“两个”文档,则必须采用这种格式。您看到的每个换行符都是
字符。这是它必须采用的格式:
如果您仅发送ONE文档,则您的间距必须完全像这样: