通过我们的Web应用程序实现Docusign API

问题描述

我想要用于发送文件信封的示例代码,用于使用指定的模板签名收件人,并且一旦所有收件人签名后文件都需要下载。我有accesstoken,带有演示沙箱登录的帐户密钥,能否在vb.net中更好地提供确切的代码,如果没有,那么在c#中提供。

2个步骤 1.与收件人传递文件(也有多个收件人) 2.一旦所有收件人签名,就取回文件,而是在发件人邮件中重播

致谢, Aravind

解决方法

要完成的工作有很多材料。您可以首先从https://github.com/docusign/code-examples-csharp下载c#启动器开始。它有31个示例。如果您想在此处https://www.docusign.com/company/events提出具体问题,可以注册我们的网络研讨会。您也可以阅读我们的指南https://developers.docusign.com/docs/esign-rest-api/how-to/。您可以在其他地方找到有用信息的地方是https://www.docusign.com/blog/developers

,

这是DocuSign的基本VB示例。

您需要添加OAuth身份验证才能在生产环境中使用它。

' DocuSign Builder example. Generated: Wed,12 Aug 2020 16:35:59 GMT
' DocuSign Ⓒ 2020. MIT License -- https://opensource.org/licenses/MIT
' @see <a href="https://developers.docusign.com">DocuSign Developer Center</a>

Imports System.Net
Imports System.Text
Imports System.IO
Imports Newtonsoft.Json.Linq

Module Program
    Const accountId As String = ""
    Const accessToken As String = ""
    Const baseUri As String = "https://demo.docusign.net"
    Const documentDir = "Assets"
    Function SendDocuSignEnvelope() As String ' RETURNS envelopeId
        ' Note: The JSON string constant uses the VB interpolated format.
        ' See https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/interpolated-strings
        ' This format enables you to use the JSON as a template within your app,and
        ' replace values with interpolated/dynamic values. Remember that string values must be 
        ' escaped per the JSON string rules. (Escape " as \")
        Dim envelopeDefinition As String = $"{{
    ""emailSubject"": ""Please sign the attached document"",""status"": ""sent"",""documents"": [
        {{
            ""name"": ""Example document"",""fileExtension"": ""pdf"",""documentId"": ""1""
        }}
    ],""recipients"": {{
        ""signers"": [
            {{
                ""email"": ""signer_email@example.com"",""name"": ""Signer's name"",""recipientId"": ""1"",""clientUserId"": ""1000"",""tabs"": {{
                    ""signHereTabs"": [
                        {{
                            ""anchorString"": ""/sig1/"",""anchorXOffset"": ""20"",""anchorUnits"": ""pixels""
                        }}
                    ]
                }}
            }}
        ]
    }}
}}"
        Dim documents = {
            (mimeType:="application/pdf",filename:="Example document",documentId:="1",diskFilename:="anchorfields.pdf")
        }

        Dim url As String = $"{baseUri}/restapi/v2.1/accounts/{accountId}/envelopes"
        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(url),HttpWebRequest)
        Dim CRLF As String = vbCrLf
        Dim boundary As String = "multipartboundary_multipartboundary"
        Dim hyphens As String = "--"
        postReq.Method = "POST"
        postReq.KeepAlive = False
        postReq.ContentType = $"multipart/form-data; boundary={boundary}"
        postReq.Accept = "application/json"
        postReq.Headers.Add("Authorization",$"Bearer {accessToken}")
        postReq.UserAgent = "DS Builder tool VB"
        ' Send request as a multipart mime message with the
        ' documents included in binary format (not Base64 encoded)
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte()
        Dim stringBuffer As String
        Dim postreqstream As Stream = postReq.GetRequestStream()
        Dim postLength As Int32 = 0
        Dim rawFilePath As String = Directory.GetCurrentDirectory()  ' \\Mac\Home\www\VB_Example\VB_Example\bin\Debug\netcoreapp3.1\
        Dim docFilePath As String = Path.GetFullPath(Path.Combine(rawFilePath,"..\..\..\" & documentDir & "\"))
        Dim document As (mimeType As String,filename As String,documentId As String,diskFilename As String)

        stringBuffer = 'Send preamble and JSON request
            hyphens & boundary & CRLF & "Content-Type: application/json" &
            CRLF & "Content-Disposition: form-data" & CRLF & CRLF & envelopeDefinition
        byteData = encoding.GetBytes(stringBuffer)
        postreqstream.Write(byteData,byteData.Length)
        postLength += byteData.Length

        For Each document In documents
            stringBuffer = CRLF & hyphens & boundary & CRLF & $"Content-Type: {document.mimeType}" &
                CRLF & $"Content-Disposition: file; filename=""{document.filename}"";documentid={document.documentId}" & CRLF & CRLF
            byteData = encoding.GetBytes(stringBuffer)
            postreqstream.Write(byteData,byteData.Length)
            postLength += byteData.Length
            ' add the file's contents
            Dim inputFile = File.Open(docFilePath & document.diskFilename,FileMode.Open)
            ' 1/2 Megabyte buffer. Dim statements specifies last index,so we subtract 1
            Dim bufferSize As Integer = 1024 * 512
            Dim bytes = New Byte(bufferSize - 1) {}
            Dim bytesRead As Int32 = inputFile.Read(bytes,bufferSize)
            While bytesRead > 0
                postreqstream.Write(bytes,bytesRead)
                postLength += bytesRead
                bytesRead = inputFile.Read(bytes,bufferSize)
            End While
            inputFile.Close()
        Next

        stringBuffer = CRLF & hyphens & boundary & hyphens & CRLF 'Send postamble
        byteData = encoding.GetBytes(stringBuffer)
        postreqstream.Write(byteData,byteData.Length)
        postLength += byteData.Length
        postReq.ContentLength = postLength

        Try
            Dim postresponse As HttpWebResponse = DirectCast(postReq.GetResponse(),HttpWebResponse)
            Dim postreqreader = New StreamReader(postresponse.GetResponseStream())
            Dim resultsJSON As String = postreqreader.ReadToEnd
            Console.WriteLine($"Create envelope results: {resultsJSON}")
            Dim resultsJObj As JObject = JObject.Parse(resultsJSON)
            Dim envelopeId As String = resultsJObj("envelopeId")
            Console.WriteLine($"EnvelopeId: {envelopeId}")
            Return envelopeId
        Catch Ex As WebException
            Console.WriteLine($"Error while creating envelope! {Ex.Message}")
            If Ex.Response IsNot Nothing Then
                Console.WriteLine($"Error response: {New StreamReader(Ex.Response.GetResponseStream).ReadToEnd}")
            End If
            Return ""
        End Try
    End Function

    Sub RecipientView(envelopeId As String)
        Dim doRecipientView As Boolean = True
        Dim recipientViewRequest As String = $"{{
    ""returnUrl"": ""https://docusign.com"",""authenticationMethod"": ""none"",""email"": ""signer_email@example.com"",""userName"": ""Signer's name""
}}"

        Dim url As String = $"{baseUri}/restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/views/recipient"
        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(url),HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = False
        postReq.ContentType = "application/json"
        postReq.Accept = "application/json"
        postReq.Headers.Add("Authorization",$"Bearer {accessToken}")
        postReq.UserAgent = "DS Builder tool VB"
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte()
        Dim postreqstream As Stream = postReq.GetRequestStream()
        byteData = encoding.GetBytes(recipientViewRequest)
        postreqstream.Write(byteData,byteData.Length)
        postReq.ContentLength = byteData.Length
        Try
            Dim postresponse As HttpWebResponse = DirectCast(postReq.GetResponse(),HttpWebResponse)
            Dim postreqreader = New StreamReader(postresponse.GetResponseStream())
            Dim resultsJSON As String = postreqreader.ReadToEnd
            Dim resultsJObj As JObject = JObject.Parse(resultsJSON)
            Dim viewUrl As String = resultsJObj("url")
            Console.WriteLine("Create recipient view succeeded.")
            Console.WriteLine("Open the signing ceremony's long URL within 5 minutes:")
            Console.WriteLine(viewUrl)
        Catch Ex As WebException
            Console.WriteLine($"Error requesting recipient view! {Ex.Message}")
            If Ex.Response IsNot Nothing Then
                Console.WriteLine($"Error response: {New StreamReader(Ex.Response.GetResponseStream).ReadToEnd}")
            End If
        End Try
    End Sub

    ' The mainline
    Sub Main(args As String())
        Console.WriteLine("Starting...")
        Dim envelopeId As String = SendDocuSignEnvelope()
        RecipientView(envelopeId)
        Console.WriteLine("Done.")
    End Sub
End Module