使用PHP进行DocusignAPI身份验证:“未指定集成器密钥”

使用PHP进行DocusignAPI身份验证:“未指定集成器密钥”

问题描述:

I'm working on my first DocuSign API implementation and I'm having trouble authenticating to the REST API.

Following the API docs, I can authenticate successfully through using curl on the command line:

$ 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": ""
    }
  ]

However, when trying to authenticate using an HTTP request through PHP, I receive a '401 Unauthorized' response, with the body:

'{
  "errorCode": "PARTNER_AUTHENTICATION_FAILED",
  "message": "The specified Integrator Key was not found or is disabled. An Integrator key was not specified."
}'

I'm pretty sure I'm setting an IntegratorKey header. My PHP code looks like:

$request = new \http\Client\Request('GET', $url, $this->getHeaders());
$client = new \http\Client();
$client->enqueue($request)->send();
$response = $client->getResponse();

I'm using the pecl/http-v2 HTTP library (docs at: http://devel-m6w6.rhcloud.com/mdref/http )

$url evals to: 'https://demo.docusign.net/restapi/v2/login_information'

The header array looks like:

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)

It appears the API key isn't being received, even though I can see the proper headers are being sent. Any ideas what I'm doing wrong here? Thanks for your help!

Edit: Fixed a missing double-quote around the password field in the authentication header. I had accidentally removed it while redacting the password. The authentication header matches character-for-character with the header used in the (working) 'curl' command.

At this point, it looks like the HTTP library I used is the source of the problem. I switched to GuzzleHttp and the API calls worked without issue.

I suspect the array where I specified the headers was not given in the correct format. It could have been a key => value format instead, but I haven't had the time/interest in testing this. Guzzle seems to be more modern and is working, so I haven't bothered going back to the pecl package.

Please Review How should the header X-DocuSign-Authentication be used for REST and SOAP? as I think your " (quotes " ") are missing for the json "key" : "value"...

and it suspect this sample may serve you from 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" )                                                                       
);