我正在尝试合并2个API演练示例,但是使用下面的代码我收到了authorization_invalid_request错误。
任何人都可以帮助我理解为什么会这样吗?
此外,我不知道这是否是最佳流程,但我想要完成的是让用户根据在我的本地数据库中为该用户保存的pdf签署不同的文档,并且动态创建模板比在docusign中预定的那样。有没有更好的方法来实现这一目标?
我的代码是:
<?php
class DocusignView {
public static function getEmbeddedSignView($signerName, $templateId, $templateRoleName, $clientUserId)
{
// Input your info:
$email = "***"; // your account email
$password = "***"; // your account password
$integratorKey = "***"; // your account integrator key, found on (Preferences -> API page)
$recipientName = '***'; // provide a recipient (signer) name
$templateId = '***'; // provide a valid templateId of a template in your account
$templateRoleName = 'Employee'; // use same role name that exists on the template in the console
$clientUserId = '1'; // to add an embedded recipient you must set their clientUserId property in addition to
// the recipient name and email. Whatever you set the clientUserId to you must use the same
// value when requesting the signing URL
// construct the authentication header:
$header = "<DocuSignCredentials><Username>" . $email . "</Username><Password>" . $password . "</Password><IntegratorKey>" . $integratorKey . "</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);
$envelopeId = DocusignView::requestSignByDoc($clientUserId);//$response["envelopeId"];
/////////////////////////////////////////////////////////////////////////////////////////////////
// STEP 2 - Get the Embedded Singing View
/////////////////////////////////////////////////////////////////////////////////////////////////
$data = array("returnUrl" => "http://www.docusign.com/devcenter",
"authenticationMethod" => "None", "email" => $email,
"userName" => $recipientName, "clientUserId" => $clientUserId
);
$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
return $url;
}
public static function requestSignByDoc($clientUserId){
// Input your info here:
$email = "***"; // your account email
$password = "***"; // your account password
$integratorKey = "***"; // your account integrator key, found on (Preferences -> API page)
$recipientName = "***"; // provide a recipient (signer) name
$documentName = "***.pdf"; // copy document with same name into this directory!
// 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 an envelope with one recipient, one tab, and one document and send
/////////////////////////////////////////////////////////////////////////////////////////////////
// the following request body will place 1 signature tab on the document you supply, located
// 100 pixels to the right and 100 pixels down from the top left of the document
$data = array (
"emailSubject" => "DocuSign API - Signature Request on Document",
"documents" => array( array( "documentId" => "1", "name" => $documentName)),
"recipients" => array( "signers" => array(
array( "email" => $email,
"name" => $recipientName,
"recipientId"=> '1',
"clientUserId" => $clientUserId,
"tabs" => array(
"signHereTabs" => array(
array( "anchorString" => "Signed .....................................................",
"anchorXOffset" => "0",
"anchorYOffset" => "1",
"anchorIgnoreIfNotPresent"=> "false",
"anchorUnits" => "inches" )
))
))
),
"status" => "created"
);
$data_string = json_encode($data);
$temp = __DIR__.'/***.pdf';
$file_contents = file_get_contents($temp);
$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=\"$documentName\"; documentid=1 \r\n"
."\r\n"
."$file_contents\r\n"
."--myboundary--\r\n"
."\r\n";
// *** 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, $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);
$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);
$envelopeId = $response["envelopeId"];
return $envelopeId;
}
}
我的JSON请求:
"{
"returnUrl":"http:\/\/www.docusign.com\/devcenter",
"authenticationMethod":"None",
"email":"***",
"userName":"***",
"clientUserId":"1"
}"
我的JSON回复:
"{
"errorCode": "AUTHORIZATION_INVALID_REQUEST",
"message": "The authorization request is malformed."
}"
我的身份验证方法设置为null,但是当我没有使用自定义信封时,运行嵌入式视图示例时没有问题(即按文档请求签名)。
并且继续尝试使用API同样的问题:
$docuSignClient = new DocuSign_Client();
$docuService = new DocuSign_ViewsService($docuSignClient);
$viewResource = new DocuSign_ViewsResource($docuService);
$signatureResource = new DocuSign_RequestSignatureResource($docuService);
$temp = __DIR__.'/test.pdf';
$file_contents = file_get_contents($temp);
$document[] = new DocuSign_Document('test.pdf', '1', $file_contents);
$recipient[] = new DocuSign_Recipient('1', '1', 'Signer1', 'recipientsEmail@email.com', '1', 'signers');
$envelopeId = $signatureResource->createEnvelopeFromDocument('TEST EMAIL SUBJECT', "PLease sign this", "created", $document, $recipient)->envelopeId;
$returnUrl = $request->getUri();
$url = $viewResource->getRecipientView($returnUrl, $envelopeId, 'Signer1', 'recipientsEmail@email.com', '1');
我错过了什么?
答案 0 :(得分:2)
因此,错误在于信封处于草稿/创建状态而未发送!有道理,但花了很长时间才弄明白。