我正在开发我的第一个DocuSign API实现,但我在向REST API进行身份验证时遇到了问题。
按照 API 文档,我可以通过在命令行上使用 curl 成功进行身份验证:
$ curl --request GET 'https://demo.docusign.net/restapi/v2/login_information' --header 'Content-Type:application/json' --header 'Accept:application/json' --header 'X-DocuSign-Authentication:{"<redacted>", "Password": "<redacted>", "IntegratorKey": "<redacted>"}'
{
"loginAccounts": [
{
"name": "<redacted>",
"accountId": "<redacted>",
"baseUrl": "https://demo.docusign.net/restapi/v2/accounts/<redacted>",
"isDefault": "true",
"userName": "<redacted>",
"userId": "<redacted>",
"email": "<redacted>",
"siteDescription": ""
}
]
但是,当尝试通过 PHP 使用 HTTP 请求进行身份验证时,我收到"401 未经授权"响应,正文如下:
'{
"errorCode": "PARTNER_AUTHENTICATION_FAILED",
"message": "The specified Integrator Key was not found or is disabled. An Integrator key was not specified."
}'
我很确定我正在设置一个集成器密钥标头。我的PHP代码看起来像:
$request = new 'http'Client'Request('GET', $url, $this->getHeaders());
$client = new 'http'Client();
$client->enqueue($request)->send();
$response = $client->getResponse();
我正在使用 pecl/http-v2 HTTP 库(文档位于:http://devel-m6w6.rhcloud.com/mdref/http
)$url评估:'https://demo.docusign.net/restapi/v2/login_information'
标头数组如下所示:
array (size=3)
0 => string 'X-DocuSign-Authentication: {"Username": "<redacted>", "Password": "<redacted>", "IntegratorKey": "<redacted>"}' (length=155)
1 => string 'Content-Type: application/json' (length=30)
2 => string 'Accept: application/json' (length=24)
似乎没有收到 API 密钥,即使我可以看到正在发送正确的标头。知道我在这里做错了什么吗?感谢您的帮助!
编辑:修复了身份验证标头中密码字段周围缺少双引号的问题。我在编辑密码时不小心删除了它。身份验证标头与(工作)"curl"命令中使用的标头逐字符匹配。
请查看标头
X-DocuSign-Authentication 应该如何用于 REST 和 SOAP? 因为我认为您的"(引号")缺少 json"键":"值"...
它怀疑此示例可能会从 http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromTemplate 为您提供服务:
$data = array("accountId" => $accountId,
"emailSubject" => "DocuSign API - Signature Request from Template",
"templateId" => $templateId,
"templateRoles" => array(
array( "email" => $email, "name" => $recipientName, "roleName" => $templateRoleName )),
"status" => "sent");
$data_string = json_encode($data);
$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',
'Content-Length: ' . strlen($data_string),
"X-DocuSign-Authentication: $header" )
);
在这一点上,看起来我使用的HTTP库是问题的根源。我切换到GuzzleHttp,API调用没有问题。
我怀疑我指定标题的数组没有以正确的格式给出。它本可以是一种键 => 值格式,但我没有时间/兴趣对此进行测试。Guzzle 似乎更现代并且正在工作,所以我没有费心回到 pecl 包。