我正在尝试使用 PHP 和 cURL 集成 DocuSign API 以检索帐户信息。我已成功获取访问令牌,但是当我向帐户端点发出 GET 请求时,收到以下错误:
API 调用期间出错:HTTP 400 - {“errorCode”:“INVALID_REQUEST_PARAMETER”,“message”:“请求至少包含一个无效参数。必须在“X-DocuSign-AppToken”标头中指定应用程序令牌。” }
授权.php:
<?php
function generateCodeVerifier($length = 128) {
$randomBytes = openssl_random_pseudo_bytes($length);
return rtrim(strtr(base64_encode($randomBytes), '+/', '-_'), '=');
}
function generateCodeChallenge($codeVerifier) {
return rtrim(strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '=');
}
$codeVerifier = generateCodeVerifier();
$codeChallenge = generateCodeChallenge($codeVerifier);
session_start();
$_SESSION['code_verifier'] = $codeVerifier;
$integrationKey = 'YOUR_INTEGRATION_KEY';
$redirectUri = 'https://yourdomain.com/callback.php';
$authorizationUrl = 'https://account-d.docusign.com/oauth/auth?' . http_build_query([
'response_type' => 'code',
'scope' => 'signature',
'client_id' => $integrationKey,
'redirect_uri' => $redirectUri,
'code_challenge' => $codeChallenge,
'code_challenge_method' => 'S256',
]);
header('Location: ' . $authorizationUrl);
exit;
?>
回调.php:
<?php
session_start();
if (!isset($_GET['code'])) {
echo 'Error: Authorization code not found';
exit;
}
$code = $_GET['code'];
$codeVerifier = $_SESSION['code_verifier'];
$integrationKey = 'YOUR_INTEGRATION_KEY';
$secretKey = 'YOUR_SECRET_KEY';
$redirectUri = 'https://yourdomain.com/callback.php';
$tokenUrl = 'https://account-d.docusign.com/oauth/token';
$data = [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri,
'code_verifier' => $codeVerifier
];
$headers = [
'Authorization: Basic ' . base64_encode("$integrationKey:$secretKey"),
'Content-Type: application/x-www-form-urlencoded'
];
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => implode("\r\n", $headers),
'content' => http_build_query($data),
]
]);
$response = file_get_contents($tokenUrl, false, $context);
if ($response === FALSE) {
$error = error_get_last();
echo 'Error during token exchange: ' . $error['message'];
exit;
}
$tokenData = json_decode($response, true);
$accessToken = $tokenData['access_token'];
// Store access token for further use
$_SESSION['access_token'] = $accessToken;
header('Location: docusign_get_account_info.php');
exit;
?>
docusign_get_account_info.php:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
if (!isset($_SESSION['access_token'])) {
echo 'Error: Access token not found';
exit;
}
$access_token = $_SESSION['access_token'];
// DocuSign API endpoint
$url = 'https://demo.docusign.net/restapi/v2.1/accounts';
// Initialize cURL
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $access_token",
"Content-Type: application/json"
));
// Execute cURL request
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
echo 'Error during API call: ' . curl_error($ch);
} else {
// Get HTTP response code
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code != 200) {
echo 'Error during API call: HTTP ' . $http_code . ' - ' . $response;
} else {
// Decode and display the response
$data = json_decode($response, true);
echo '<pre>';
print_r($data);
echo '</pre>';
}
}
// Close cURL session
curl_close($ch);
?>
我已经证实:
-访问令牌有效且未过期。 - 端点 URL 正确。 -我包括必要的标头(授权和内容类型)。
但是,错误消息表明我需要包含 X-DocuSign-AppToken 标头,该端点的官方 DocuSign 文档中未提及该标头。
您能帮我理解为什么会出现此错误以及如何解决它吗?
感谢您的协助。
致以诚挚的问候,
据我所知,您使用的端点不会解析为资源。获得访问令牌后,您将考虑使用 userinfo 端点。这将返回当前经过身份验证的用户的信息,包括他们的帐户信息。