1

I'm doing a POC to demonstrate DocuSign programmatically creating and routing an envelope containing a simple document. I'm using PowerShell and the JSON API. Login and the Create Envelope work without complaint, but the resulting Word doc routed to me for signature contains gibberish. I believe I have the base64 encoding and headers right. Any thoughts about what I'm doing wrong?

enter image description here

The entire POC is pasted below. I've just removed the ID, Password, Integrator Key, etc. Thanks!

function boundry {
    [System.Guid]::NewGuid().ToString()
}

###this is the corrected code###
function encodeFile {
    param ([string]$fileName)
    [System.Convert]::ToBase64String([IO.File]::ReadAllBytes((Resolve-Path $fileName).ProviderPath))
}

function logonParams {
    [string] $userName = 'DocuSign user name' 
    [string] $password = 'DocuSign password'
    [string] $integratorKey = 'DocuSign Integrator Key'

    @"
        {    
            "Username" : "$userName",
            "Password" : "$password",
            "IntegratorKey" : "$integratorKey"
        }
"@
}

function logon {
    [string] $loginURL = 'https://demo.docusign.net/restapi/v2/login_information'
    $headers = 
        @{
            "X-DocuSign-Authentication"=$(logonParams);
            "accept"="application/json";
            "content-type"="application/json";
        }

    $r = Invoke-WebRequest -uri $loginURL -headers $headers -method GET 
    $responseInfo = $r.content | ConvertFrom-Json 
    $baseURL = $responseInfo.loginAccounts.baseURL

    #return the base URL for the next call
    $baseURL
}

function createEnvelope {
    param ([string]$file1,
            [string]$baseURL
          )

    [string]$boundry = boundry
    $headers = 
    @{
        "X-DocuSign-Authentication"=$(logonParams);
        "accept"="application/json";
        "content-type"="multipart/form-data; boundary=$boundry";
    }

    [string]$formData = @"
--$boundry
Content-Type: application/json

{
  "status":"sent",
  "emailBlurb":"Please sign.",
  "emailSubject": "Contract $(date)",
  "documents": [{
      "name": "$file1",
      "documentId":"1",
      "order":"1"
  }],
  "recipients": {
    "signers" : [{
      "email": "recipient@somecompany.com",
      "name": "Recipient Name",
      "recipientId":"1",
    }]
  }
}
--$boundry
Content-Type: application/msword
Content-Transfer-Encoding: base64
Content-Disposition: file; filename="$file1";documentid=1

$(encodeFile $file1)

--$boundry--
"@

    $envelopeURL = "$baseURL/envelopes"

    Invoke-WebRequest -uri $envelopeURL -headers $headers -body $formData -method POST
}

$baseURL = logon
createEnvelope "test.doc" $baseURL
Dan Loughney
  • 4,647
  • 3
  • 25
  • 40
  • 1
    Not able to try and reproduce this right now - but from a visual inspection: the closing boundary after the encoded bytes could use another trailing dash. Also, please try to put a blank line between the end of the base64 bytes and the --$boundry--. – Luis Jun 17 '15 at 18:50
  • Thanks Luis for the suggestion. I tried, but still no luck. The code above has been updated to match suggestions. Result is the same. – Dan Loughney Jun 18 '15 at 16:00

2 Answers2

0

Try changing your Content-Type header value. I'm not sure if application/msword works here, I think the proper mime-type for .docx is

application/vnd.openxmlformats-officedocument.wordprocessingml.document

See this previous SO post for a more complete list of mime-types:

What is a correct mime type for docx, pptx etc?

Community
  • 1
  • 1
Ergin
  • 9,254
  • 1
  • 19
  • 28
  • Thanks Ergin. Still no luck. I am upload a classic .DOC file for testing, but the newer DOCX mime-type didn't change the output. Which is telling in itself. The output should have looked a little different. It really does look like an MS Word doc where the extension has been set to .PDF incorrectly. It's as if the DocsSign side has skipped the converter step. – Dan Loughney Jun 19 '15 at 01:19
  • 1. Have you tried with other word docs and or PDFs? 2. Have you tried not base64 encoding the bytes? The [API Walkthroughs](http://iodocs.docusign.com/APIWalkthroughs) don't base64 encode and I've tested those and know they work. – Ergin Jun 19 '15 at 05:32
  • I'm starting to think this is somehow related to my account. Uploaded some different PDFs and they all failed because they are "protected" when they are not. I also tried a couple DOCX files with your content-type and received "System was unable to convert this document to a PDF... Could not submit Document to Conversion Server." – Dan Loughney Jun 19 '15 at 15:32
0

I solved it with assistance from the DocuSign support team. You can enable server side logging in DocuSign which is very helpful. From the old UI (not available in new UI as of June '15) choose Preferences from the dropdown next to your ID/photo. Then select Permissions under Member Options on the left. Check "Enable API Request Logging." After you run your test, the Download API Request Logs button becomes active.

It was pretty clear from the logs that my encoding was wrong. Here's the correct version:

function encodeFile {
    param ([string]$fileName)
    [System.Convert]::ToBase64String([IO.File]::ReadAllBytes((Resolve-Path $fileName).ProviderPath))
}

I've updated this in the original code with the question so feel free to use it.

Dan Loughney
  • 4,647
  • 3
  • 25
  • 40