Docusign REST API 获取文档信封的收件人 URL

Docusign REST API Getting recipient URL for document envelope

我有一个网络应用程序,可以让用户填写一系列表格,然后生成包含他们的答案的 pdf 文件。我想在 pdf 表单的末尾添加一个 Docusign 签名,以便在完成在线表单后要求他们在 iframe 中签名。

我从这个文档开始 - https://www.docusign.com/developer-center/recipes/signing-from-your-app and then took step 2 from this doc to create my envelope with a document - https://www.docusign.com/developer-center/recipes/request-a-signature-via-email

我的代码很好地创建了信封 ID,但是当我尝试使用 /envelopes/$envelopeId/views/recipient 获取 URL 时,我收到状态 400 错误:

{ "errorCode": "ACCOUNT_NOT_AUTHORIZED_FOR_ENVELOPE", "message": "This account is not authorized to access the requested envelope." }

下面是我的完整代码:

<?php

$docusign_username = "my@username.com";
$docusign_password = "mypassword";
$docusign_integrator_key = "my-integrator-key";

$applicant_email = "johnsmith@emailaddress.com";
$applicant_name = "John Smith";
$applicant_unique_id = "123";

$application_unique_id = "31587";
$application_form_pdf = "31587.pdf";

// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $docusign_username . "</Username><Password>" . $docusign_password . "</Password><IntegratorKey>" . $docusign_integrator_key . "</IntegratorKey></DocuSignCredentials>";

//////////////////////////////////////////////////////////////////////////////////////
// STEP 1 - Login (retrieves 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 "accountId = " . $accountId . "\nbaseUrl = " . $baseUrl . "\n";


//////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Create an envelope with document
//////////////////////////////////////////////////////////////////////////////////////
$data =
    array (
        "emailSubject" => "DocuSign API - Please sign " . $application_form_pdf,
        "documents" => array(
            array("documentId" => $application_unique_id, "name" => $application_form_pdf)
            ),
        "recipients" => array(
            "signers" => array(
                array(
                    "email" => $applicant_email,
                    "name" => $applicant_name,
                    "clientUserId" => $applicant_unique_id,
                    "recipientId" => $applicant_unique_id,
                    "tabs" => array(
                        "signHereTabs" => array(
                            array(
                                "xPosition" => "100",
                                "yPosition" => "100",
                                "documentId" => $application_unique_id,
                                "pageNumber" => "1"
                            )
                        )
                    )
                )
            )
        )
    , "status" => "sent"
    // , "status" => "created"
);
$data_string = json_encode($data);
$file_contents = file_get_contents($application_form_pdf);

// Create a multi-part request. First the form data, then the file content
$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=\"$application_form_pdf\"; documentid=".$application_unique_id." \r\n"
    ."\r\n"
    ."$file_contents\r\n"
    ."--myboundary--\r\n"
    ."\r\n";

// Send to the /envelopes end point, which is relative to the baseUrl received above.
$curl = curl_init($baseUrl . "/envelopes" );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
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); // Do it!

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
    echo "Error calling DocuSign, status is:" . $status . "\nerror text: ";
    print_r($json_response); echo "\n";
    exit(-1);
}

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

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

//////////////////////////////////////////////////////////////////////////////////////
// STEP 3 - Get the Embedded Signing View
//////////////////////////////////////////////////////////////////////////////////////
$data = array(
      "returnUrl" => "https://www.docusign.com/devcenter"
    , "authenticationMethod" => "None"
    , "authenticationInstant" => "None"
    , "userId" => $applicant_unique_id
    , "clientUserId" => $applicant_unique_id
    // , "email" => $applicant_email
    // , "userName" => $applicant_name
);

$data_string = json_encode($data);
$curl = curl_init($baseUrl."/envelopes/$envelopeId/views/recipient" );
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',
    'Content-Length: ' . strlen($data_string),
    "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";
    exit(-1);
}

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

//--- display results
echo "Embedded URL is: \n\n" . $url . "\n\nNavigate to this URL to start the embedded signing view of the envelope\n";

?>

如有任何想法,我们将不胜感激,谢谢!

获取收件人视图(签名视图)时,尝试通过指定

来识别签名者
"email" => $email, # signer's email
"userName" => $recipientName,
"clientUserId" => $clientUserId

如嵌入式签名配方所示。

不要设置 userId