Skip to main content
Newcomer
October 4, 2024
Question

AUTHORIZATION_INVALID_TOKEN, The access token provided is expired, revoked or malformed

  • October 4, 2024
  • 3 replies
  • 244 views

Hi,

I’m trying with PHP but always getting this error my code like this

<?php

require_once 'vendor/autoload.php';

 

use DocuSign\eSign\Client\ApiClient;

use DocuSign\eSign\Configuration;

use DocuSign\eSign\Api\EnvelopesApi;

use DocuSign\eSign\Model\EnvelopeDefinition;

use DocuSign\eSign\Model\Signer;

use DocuSign\eSign\Model\Recipients;

use DocuSign\eSign\Model\Document;

use Firebase\JWT\JWT;

 

$integrationKey = '9eaed807-bd9f-4d38-b858-37f6eccc6b73'; // Your Integration Key

$userId = 'd4300ed6-aa42-47cd-b687-01c825726abd'; // Your User ID (GUID)

$privateKey = file_get_contents('key.pem'); // Path to your private key

$accountId = '8c637b59-afa2-4821-8c5d-981bacf4ddde'; // Your Account ID

 

// Set the current time and expiration time

$now = time();

$expiration = $now + 3600000; // Token valid for 1 hour

 

// Create the JWT payload

$payload = [

    'iss' => $integrationKey,

    'sub' => $userId,

    'iat' => $now,

    'exp' => $expiration,

    'aud' => 'demo.docusign.net',  //aud: Should be set to account.docusign.com for production or demo.docusign.net for the demo environment.

    'scope' => 'signature impersonation'

];

 

// Generate the JWT

$jwt = JWT::encode($payload, $privateKey, 'RS256');

 

// Create a new ApiClient and configure it

$config = new Configuration();

$config->setHost('https://demo.docusign.net/restapi'); // Use demo for sandbox

$apiClient = new ApiClient($config);

 

// Add the Authorization header

$apiClient->getConfig()->addDefaultHeader('Authorization', 'Bearer ' . $jwt);

 

// Create an envelope definition

$envelopeDefinition = new EnvelopeDefinition();

$envelopeDefinition->setEmailSubject("Please sign this document");

 

// Create a document

$document = new Document();

$document->setDocumentBase64(base64_encode(file_get_contents('test.pdf')));

$document->setName('Sample Document');

$document->setFileExtension('pdf');

$document->setDocumentId('1');

 

$envelopeDefinition->setDocuments([$document]);

 

// Create a signer

$signer = new Signer();

$signer->setEmail('test@gmail.com');

$signer->setName('Uday');

$signer->setRecipientId('1');

 

// Add signer to recipients

$recipients = new Recipients();

$recipients->setSigners([$signer]);

 

// Add recipients to envelope

$envelopeDefinition->setRecipients($recipients);

 

// Set envelope status to 'sent'

$envelopeDefinition->setStatus('sent');

 

// Create EnvelopesApi instance

$envelopesApi = new EnvelopesApi($apiClient);

 

try {

    // Send the envelope

    $result = $envelopesApi->createEnvelope($accountId, $envelopeDefinition);

    echo "Envelope has been sent successfully! Envelope ID: " . $result->getEnvelopeId();

} catch (Exception $e) {

    echo "Error sending envelope: " . $e->getMessage();

}

 

3 replies

Hengfeng Ge
Hero
Hero
October 4, 2024

aud' => 'account-d.docusign.comt',  //aud: Should be set to account.docusign.com for production or account-d.docusign.com for the demo environment.

FreeLink/甫连信息
🌍 DocuSign Partner | Partner Profile
🏆 2024 APAC Reseller Growth Partner of the Year
🔧 The first in APAC to pass the DocuSign eSignature Technical Consultant certification.
🚀 Expertise in DocuSign integrations with on-premises systems for leading enterprises across various industries.

Feel free to reach out for collaboration opportunities.

 

Welcome to the DocuSign Community! Your feedback is highly valued. If you find my response helpful, please give it a "Like" and consider marking it as the "Best Answer" to assist others with similar issues.
Newcomer
October 5, 2024

Hi 

Now sending same but getting same error

$payload = [

    'iss' => $integrationKey,

    'sub' => $userId,

    'iat' => $now,

    'exp' => $expiration,

    'aud' => 'account-d.docusign.com',  

    'scope' => 'signature impersonation'

];

Hengfeng Ge
Hero
Hero
October 5, 2024

$jwt is jwt assertion, not Bearer token, you should use $jwt to get Bearer token, then set it in the header. I find also other issue in the code.

 

FreeLink/甫连信息
🌍 DocuSign Partner | Partner Profile
🏆 2024 APAC Reseller Growth Partner of the Year
🔧 The first in APAC to pass the DocuSign eSignature Technical Consultant certification.
🚀 Expertise in DocuSign integrations with on-premises systems for leading enterprises across various industries.

Feel free to reach out for collaboration opportunities.

Welcome to the DocuSign Community! Your feedback is highly valued. If you find my response helpful, please give it a "Like" and consider marking it as the "Best Answer" to assist others with similar issues.