docusign / docusign-esign-php-client

The Official Docusign PHP Client Library used to interact with the eSign REST API. Send, sign, and approve documents using this client.
https://www.docusign.com/devcenter
MIT License
198 stars 123 forks source link

JWT Authentication failed #230

Closed yugandhararao closed 3 weeks ago

yugandhararao commented 3 weeks ago

We are integrating Docusign with Moodle, it is showing the below error we want to get an access token. require_once(DIR.'/../../config.php'); require_once('../../vendor/autoload.php'); use DocuSign\eSign\Client\ApiClient; use DocuSign\eSign\Client\ApiException; use DocuSign\eSign\Configuration; use DocuSign\eSign\Api\EnvelopesApi; use DocuSign\eSign\Model\EnvelopeDefinition; use DocuSign\eSign\Model\Document; use DocuSign\eSign\Model\Signer; use DocuSign\eSign\Model\Recipients; use DocuSign\eSign\Model\Tabs; use DocuSign\eSign\Model\SignHere; use DocuSign\eSign\Model\RecipientViewRequest; $client_id = 'xxxx'; $user_id = 'xxxx'; $oauth_base_path = 'account-d.docusign.com'; $private_key = file_get_contents('private1.key'); $base_path = 'https://demo.docusign.net/restapi'; $expires_in_seconds = 3600; // Token validity period $scopes = 'signature impersonation'; // Initialize the API client $config = new Configuration(); $apiClient = new ApiClient($config); // Generate JWT and get the access token try { $apiClient->getOAuth()->setOAuthBasePath($oauth_base_path); $response = $apiClient->requestJWTUserToken($client_id, $user_id, $private_key, ['signature impersonation'], $expires_in_seconds); $access_token = $response[0]->getAccessToken(); $info = $apiClient->getUserInfo($access_token); $account_id = $info[0]['accounts'][0]["account_id"]; } catch (ApiException $e) { echo 'JWT Authentication failed: ', $e->getMessage(), PHP_EOL; if(strpos($e->getMessage(),'consent_required') !== false){ $authorizationURL = 'https://account-d.docusign.com/oauth/auth?'. http_build_query([ 'scope' => $scopes, 'redirect_uri' => 'http://localhost/moodle4', 'client_id' => $client_id, 'response_type' => 'code' ]); echo $authorizationURL; exit; }
}

Error: JWT Authentication failed: Error while requesting server, received a non successful HTTP code [400] with response Body: O:8:"stdClass":2:{s:5:"error";s:13:"invalid_grant";s:17:"error_description";s:22:"unsupported_grant_type";}

avinfinity commented 3 weeks ago

@yugandhararao : Could you please confirm if you have obtained the required consent , before requesting JWT token. For more details please look at below link: https://developers.docusign.com/platform/auth/consent/obtaining-individual-consent/

Also could you please try passing scopes as string array like below: $scopes = ['signature', 'impersonation'];

garg-mudit commented 3 weeks ago

@yugandhararao Can you please also confirm if your IK is set for proper grant type (i.e. auth code grant and not implicit grant).

Thank You.

yugandhararao commented 3 weeks ago

Thank you both of you!! Resolved Moodle conflict with DocuSign jwt issue.

garg-mudit commented 3 weeks ago

@yugandhararao May I know what the issue was ?

yugandhararao commented 3 weeks ago

@garg-mudit The issue arose because Moodle and DocuSign were loading the same packages, such as JWT. I commented out the config.php file, and now it's working fine.