1

有没有人按照 REST API 中的示例成功地将文档添加到草稿信封中?

  1. 将文档添加到草稿信封中 - 我根本无法使用它。
  2. 将文档添加到草稿信封 - 我取得了部分成功。在 Web 控制台上,我可以看到文档已添加到信封中,但在尝试打开文档时,文档显示“无法加载 PDF”。文档的链接看起来像https://demo.docusign.net/Member/noname.pdf?d=00000000-0000-0000-0000-000000000000&...

我用于#2 的 PUT 请求:

X-DocuSign-Authentication: {"SendOnBehalfOf": "xxx", "Username":"xxx","Password":"xxx","IntegratorKey":"xxx"} Accept: application/json Content-Type: application/ pdf 内容处置:文件;文件名="api_upload.pdf"; 文档ID=3;fileExtension="pdf" 内容传输编码:base64

[删除了 base64 编码字节]

任何建议将不胜感激。

4

1 回答 1

0

我刚刚测试并能够使用以下 PHP 代码将两个文档添加到草稿信封中。如果您输入您的凭据,此代码将登录,创建草稿信封,然后一次性将 2 个文档添加到该草稿信封中。

要运行此代码:

  1. 在本地保存文件。
  2. 在顶部输入凭据。
  3. 将两个 pdf 文件复制到同一目录,分别命名为 document1.pdf 和 document2.pdf。
  4. 运行代码。

注意:此调用的 REST API 文档有一些不准确之处,以下请求正文有效,因此请遵循此...

<?php

// Input your info here:
$integratorKey = '...';
$email = '...';
$password = '...';
$name = 'John Doe';

// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</IntegratorKey></DocuSignCredentials>";

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (to retrieve baseUrl and accountId)
/////////////////////////////////////////////////////////////////////////////////////////////////
$url = "https://demo.docusign.net/restapi/v2/login_information";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-DocuSign-Authentication: $header"));

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 200 ) {
    echo "error calling webservice, status is:" . $status;
    exit(-1);
}

$response = json_decode($json_response, true);
$accountId = $response["loginAccounts"][0]["accountId"];
$baseUrl = $response["loginAccounts"][0]["baseUrl"];
curl_close($curl);

//--- display results
echo "\naccountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create a draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
    "emailBlurb" => "This comes from PHP",
    "emailSubject" => "DocuSign API Testing",
    "status" => "created");   
$data_string = json_encode($data);  

// *** append "/envelopes" to baseUrl and as signature request endpoint
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',
    "X-DocuSign-Authentication: $header" )                                                                       
);

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
    print_r($json_response); echo "\n";
    return;
}

$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];

//--- display results
echo "Document is created! Envelope ID = " . $envelopeId . "\n\n"; 

/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Add documents to draft envelope
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array (
    "documents" => array( 
        array( "documentId" => "1", "name" => "document1.pdf", "order" => "1" ),
        array( "documentId" => "2", "name" => "document2.pdf", "order" => "2" )
    ));  
$data_string = json_encode($data);  

$file_contents1 = file_get_contents("document1.pdf");
$file_contents2 = file_get_contents("document2.pdf");

$requestBody = "\r\n"
."\r\n"
."--myboundary\r\n"
."Content-Type: application/json\r\n"
."Content-Disposition: form-data\r\n"
."\r\n"
."$data_string\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document1.pdf\"; documentid=1 \r\n"
."\r\n"
."$file_contents1\r\n"
."--myboundary\r\n"
."Content-Type:application/pdf\r\n"
."Content-Disposition: file; filename=\"document2.pdf\"; documentid=2 \r\n"
."\r\n"
."$file_contents2\r\n"
."--myboundary--\r\n"
."\r\n";

// *** append "/envelopes/{envelopeId}/documents" to baseUrl and as endpoint
$url = $baseUrl . "/envelopes/$envelopeId/documents";
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);                                                                  
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: multipart/form-data;boundary=myboundary',
    'Content-Length: ' . strlen($requestBody),
    "X-DocuSign-Authentication: $header" )                                                                       
);

$json_response = curl_exec($curl);
$response = json_decode($json_response, true);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    echo "error calling webservice, status is:" . $status . "\nerror text is --> ";
    print_r($response); echo "\n";
    exit(-1);
}

$response = json_decode($json_response, true);
$envelopeId = $response["envelopeId"];

//--- display results
echo "Document is sent! Envelope ID = " . $envelopeId . "\n\n"; 
?>

我要注意的最后一件事是,确保您正在为此调用发出 PUT 请求而不是 POST,因为这是我在这里看到的常见错误。

于 2013-10-07T18:14:02.263 回答