新DocuSign信封中的损坏文件

时间:2015-06-17 17:17:18

标签: rest powershell file-upload docusignapi

我正在做一个POC来演示DocuSign以编程方式创建和路由包含简单文档的信封。我正在使用PowerShell和JSON API。登录和创建信封无需投诉,但生成的Word文档路由到我签名包含乱码。我相信我有base64编码和标题正确。关于我做错了什么的任何想法?

enter image description here

整个POC粘贴在下面。我刚删除了ID,密码,集成键等等。谢谢!

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

2 个答案:

答案 0 :(得分:0)

尝试更改Content-Type标头值。我不确定application/msword是否在这里工作,我认为.docx的正确mime类型是

application/vnd.openxmlformats-officedocument.wordprocessingml.document

有关mime-types的更完整列表,请参阅此前的SO帖子:

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

答案 1 :(得分:0)

我在DocuSign支持团队的帮助下解决了这个问题。您可以在DocuSign中启用服务器端日志记录,这非常有用。从旧的用户界面(截至6月15日在新用户界面中不可用),从您的ID /照片旁边的下拉列表中选择偏好设置。然后在左侧的“成员选项”下选择“权限”。选中"启用API请求记录。"运行测试后,“下载API请求日志”按钮将变为活动状态。

从日志中可以清楚地看出我的编码错误。这是正确的版本:

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

我已在原始代码中更新了此问题,因此请随时使用。