如何使用 JWT 和 PHP 中的 SDK 获取 Docusign API 访问令牌?
How to get a Docusign API access token using JWT with the SDK in PHP?
所以,我想我已经在我的帐户中设置了所有内容,因为我已经有了我的用户 ID、客户端 ID 和私钥。根据 documentation,我应该能够导入 ApiClient.php
并调用 requestJWTUserToken
来获取访问令牌。这是现在的样子
$config = new Configuration();
$apiClient = new ApiClient($config);
$client_id = config('docusign.integration_key');
$user_id = config('docusign.user_id');
$rsa_private_key = openssl_pkey_get_private("file://docusign.pem");
$apiClient->requestJWTUserToken($client_id, $user_id, $rsa_private_key);
但是,我收到一条错误消息
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:16:"issuer_not_found";}
at vendor/docusign/esign-client/src/Client/ApiClient.php:344
340| if(is_object($serializedData)){
341| $serializedData = serialize($serializedData);
342| }
343|
> 344| throw new ApiException(
345| "Error while requesting server, received a non successful HTTP code [".$response_info['http_code']."] with response Body: $serializedData",
346| $response_info['http_code'],
347| $http_header,
348| $data
知道发生了什么吗?
如果您将 Windows 与 Git Bash 一起使用,请尝试此代码。我会下载 PHP Quickstart、运行 composer install
并将此文件放入根文件夹。
请务必在您的 DocuSign 开发者帐户的应用中添加 https://httpbin.org/get
的重定向 URI。此外,将您的集成密钥和用户 ID 粘贴到此文件中。在您的 ds_config.php
文件中,将 quickstart
设置为 false
。
<?php
require "vendor/autoload.php";
use DocuSign\eSign\Configuration;
use DocuSign\eSign\Api\EnvelopesApi;
use DocuSign\eSign\Client\ApiClient;
use DocuSign\eSign\Client\ApiException;
use DocuSign\eSign\Api\EnvelopesApi\ListStatusChangesOptions;
$rsaPrivateKey = file_get_contents("./private.key");
$integration_key = "";
$impersonatedUserId = "";
$scopes = "signature impersonation";
$config = new Configuration();
$apiClient = new ApiClient($config);
try {
$apiClient->getOAuth()->setOAuthBasePath("account-d.docusign.com");
$response = $apiClient->requestJWTUserToken($integration_key, $impersonatedUserId, $rsaPrivateKey, $scopes, 60);
} catch (\Throwable $th) {
var_dump($th);
// we found consent_required in the response body meaning first time consent is needed
if (strpos($th->getMessage(), "consent_required") !== false) {
$authorizationURL = 'https://account-d.docusign.com/oauth/auth?' . http_build_query([
'scope' => $scopes,
'redirect_uri' => 'https://httpbin.org/get',
'client_id' => $integration_key,
'response_type' => 'code'
]);
echo "It appears that you are using this integration key for the first time. Please visit the following link to grant consent authorization.";
echo "\n\n";
echo $authorizationURL;
exit();
}
}
if (isset($response)) {
$access_token = $response[0]['access_token'];
// retrieve our API account Id
$info = $apiClient->getUserInfo($access_token);
$account_id = $info[0]["accounts"][0]["account_id"];
// Instantiate the API client again with the default header set to the access token
$config->setHost("https://demo.docusign.net/restapi");
$config->addDefaultHeader('Authorization', 'Bearer ' . $access_token);
$apiClient = new ApiClient($config);
$envelope_api = new EnvelopesApi($apiClient);
$from_date = date("c", (time() - (30 * 24 * 60 * 60)));
$options = new ListStatusChangesOptions();
$options->setFromDate($from_date);
try {
$results = $envelope_api->listStatusChanges($account_id, $options);
echo "results";
echo "\n\n";
echo stripslashes($results);
} catch (ApiException $e) {
var_dump($e);
exit;
}
}
所以,我想我已经在我的帐户中设置了所有内容,因为我已经有了我的用户 ID、客户端 ID 和私钥。根据 documentation,我应该能够导入 ApiClient.php
并调用 requestJWTUserToken
来获取访问令牌。这是现在的样子
$config = new Configuration();
$apiClient = new ApiClient($config);
$client_id = config('docusign.integration_key');
$user_id = config('docusign.user_id');
$rsa_private_key = openssl_pkey_get_private("file://docusign.pem");
$apiClient->requestJWTUserToken($client_id, $user_id, $rsa_private_key);
但是,我收到一条错误消息
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:16:"issuer_not_found";}
at vendor/docusign/esign-client/src/Client/ApiClient.php:344
340| if(is_object($serializedData)){
341| $serializedData = serialize($serializedData);
342| }
343|
> 344| throw new ApiException(
345| "Error while requesting server, received a non successful HTTP code [".$response_info['http_code']."] with response Body: $serializedData",
346| $response_info['http_code'],
347| $http_header,
348| $data
知道发生了什么吗?
如果您将 Windows 与 Git Bash 一起使用,请尝试此代码。我会下载 PHP Quickstart、运行 composer install
并将此文件放入根文件夹。
请务必在您的 DocuSign 开发者帐户的应用中添加 https://httpbin.org/get
的重定向 URI。此外,将您的集成密钥和用户 ID 粘贴到此文件中。在您的 ds_config.php
文件中,将 quickstart
设置为 false
。
<?php
require "vendor/autoload.php";
use DocuSign\eSign\Configuration;
use DocuSign\eSign\Api\EnvelopesApi;
use DocuSign\eSign\Client\ApiClient;
use DocuSign\eSign\Client\ApiException;
use DocuSign\eSign\Api\EnvelopesApi\ListStatusChangesOptions;
$rsaPrivateKey = file_get_contents("./private.key");
$integration_key = "";
$impersonatedUserId = "";
$scopes = "signature impersonation";
$config = new Configuration();
$apiClient = new ApiClient($config);
try {
$apiClient->getOAuth()->setOAuthBasePath("account-d.docusign.com");
$response = $apiClient->requestJWTUserToken($integration_key, $impersonatedUserId, $rsaPrivateKey, $scopes, 60);
} catch (\Throwable $th) {
var_dump($th);
// we found consent_required in the response body meaning first time consent is needed
if (strpos($th->getMessage(), "consent_required") !== false) {
$authorizationURL = 'https://account-d.docusign.com/oauth/auth?' . http_build_query([
'scope' => $scopes,
'redirect_uri' => 'https://httpbin.org/get',
'client_id' => $integration_key,
'response_type' => 'code'
]);
echo "It appears that you are using this integration key for the first time. Please visit the following link to grant consent authorization.";
echo "\n\n";
echo $authorizationURL;
exit();
}
}
if (isset($response)) {
$access_token = $response[0]['access_token'];
// retrieve our API account Id
$info = $apiClient->getUserInfo($access_token);
$account_id = $info[0]["accounts"][0]["account_id"];
// Instantiate the API client again with the default header set to the access token
$config->setHost("https://demo.docusign.net/restapi");
$config->addDefaultHeader('Authorization', 'Bearer ' . $access_token);
$apiClient = new ApiClient($config);
$envelope_api = new EnvelopesApi($apiClient);
$from_date = date("c", (time() - (30 * 24 * 60 * 60)));
$options = new ListStatusChangesOptions();
$options->setFromDate($from_date);
try {
$results = $envelope_api->listStatusChanges($account_id, $options);
echo "results";
echo "\n\n";
echo stripslashes($results);
} catch (ApiException $e) {
var_dump($e);
exit;
}
}