DLK
DLK

Reputation: 360

Create Envelope with access token returns 404 (DocuSign PHP SDK)

I am trying to create an envelope through PHP sdk, we have working integration using X-DocuSign-Authentication header (with user, password, integrator key combo). Trying to migrate the integration to access token, but keep on getting 404 Resource not found error from the actual SDK (the resources is dictated by the SDK).

Current code:

// DocuSign\eSign\Configuration
$config = new Configuration();
$config->setHost('https://www.docusign.net/restapi');

// DocuSign\eSign\Client\ApiClient
$api = new ApiClient($config);

try {
    $response = $api->requestJWTUserToken(
        "correct-integrators-key",
        "correct-user-id",
        file_get_contents( base_path() . '/ds-private.key', true), //exists
        "signature impersonation",
    );
} 
catch (ApiException $e) {
    return $e->getMessage();
}

JWT Token payload comes back successfully, and access token is valid.

// DocuSign\eSign\Client\Auth\OAuthToken
if(!$response[0] instanceof OAuthToken)
    return "Auth Token Invalid.";

$access_token = $response[0]->getAccessToken();

try {
    $user = $api->getUserInfo($access_token);
} catch (ApiException $e) {
    return $e->getMessage();
}

// DocuSign\eSign\Client\Auth\UserInfo
if(!$user[0] instanceof UserInfo)
    return "User Info Invalid.";

Setting the account ID and base URL also are seemingly correct (account ID comes back as expected, and is correct one, base URL comes back as na2 subdomain, seems to be the correct - this is supported by the fact that "USER_DOES_NOT_BELONG_TO_SPECIFIED_ACCOUNT" is thrown if any other host is used)

$account_id = null;
$base_url = null;

foreach ($user[0]->getAccounts() as $account) {
    if($account instanceof Account)
        if($account->getIsDefault()) {
            $account_id = $account->getAccountId(); // Account ID succeeds, comes back as correct account ID (verified on the admin panel)
            $base_url = $account->getBaseUri(); // Base URL succeeds, comes back as na2 subdomain
        }
}

$config->setAccessToken($access_token); // Access token succeeds

$config->setHost($base_url); 

This code is practically copy/paste of working example with the "old" integration.

$envelopeApi = new EnvelopesApi($api);

$templateRole = new TemplateRole();
$definition = new EnvelopeDefinition();

$templateRole->setEmail('[email protected]');
$templateRole->setName('Rebecca Smith');
$templateRole->setRoleName('Correct Role Defined On Template');
$templateRole->setClientUserId('Correct User Id For Embedding');

$signers = [];
$signers[] = $templateRole;

$definition->setTemplateId('Valid Template Id');
$definition->setTemplateRoles($signers);
$definition->setStatus('sent');

try {
    $envelope = $envelopeApi->createEnvelope($account_id, $definition);
}
catch (ApiException $e) {
    return [
        'envelope_error_message' => $e->getMessage(), // Returns: "Error while requesting server, received a non successful HTTP code [404] with response Body:  "
        'envelope_error_code' => $e->getCode(), // Returns: 404
    ];
}

Tried also directly running $api->callApi to check if v2.1 vs v2 in resource path is the issue, but got 404 on both.

Upvotes: 1

Views: 628

Answers (1)

Larry K
Larry K

Reputation: 49114

You need to append /restapi to the baseUri

Instead of

$base_url = $account->getBaseUri(); // Base URL succeeds, comes back as na2 subdomain

try

$base_uri_suffix = '/restapi';
$base_url = $account->getBaseUri().$base_uri_suffix; // Base URL succeeds, comes back as na2 subdomain

See the source in the PHP Code Example

Upvotes: 4

Related Questions