首页 > 解决方案 > DocuSign JWT 响应错误 invalid_request 使用 PHP

问题描述

我在尝试从 DocuSign 检索 JWT 令牌时遇到问题,到目前为止,这是我已完成的代码片段(使用 DEVELOPER SANDBOX 帐户):

// Developer exmple
$header = [
    'typ' => 'JWT',
    'alg' => 'RS256',
];
$time = time();
$body = [
    // Integration key provded by DocuSign at Admin > API and Keys > Apps.
    'iss' => '[Integration key]',
    // User ID provded by DocuSign at Admin > API and Keys.
    'sub' => '[User ID]',
    'iat' => $time,
    'exp' => strtotime( '+45 minutes', $time ),
    'aud' => 'account-d.docusign.com',
    'scope' => 'signature impersonation',
];

// RSA Private key provided by DocuSign
// When integration APP was created
$rsa_key = '-----BEGIN RSA PRIVATE KEY----- [.........]';

// Base64 + URL Encoding
$header = urlencode( base64_encode( json_encode( $header ) ) );
$body = urlencode( base64_encode( json_encode( $body ) ) );

// JWT signature created using Firebase\JWT\JWT package
$signature = JWT::encode(
    $header . '.' . $body,
    $rsa_key,
    'RS256'
);

// Get request using Curl (10quality/php-curl package)
$response = curl_request(
    'https://account-d.docusign.com/oauth/token',
    'POST',
    [
        'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion' => $header . '.' . $body . '.' . $signature,
    ]
);

// Process response
if ( $response ) {
    $response = json_decode( $response );
    if ( isset( $response->error ) )
        throw new Exception( 'DocuSign error: ' . $response->error );
    var_dump( $response );
}

这是我遵循的指南: https ://developers.docusign.com/esign-rest-api/guides/authentication/oauth2-jsonwebtoken

根据本指南,抛出的错误表明 JWT 没有正确创建,尽管我已经多次查看我的代码并且它遵循指南中描述的所有内容。

我有点卡住了,我对我的代码有什么问题一无所知,我什至做了逆向工程来验证编码是否正确。

有没有人使用过 DocuSign JWT 或者知道我可能做错了什么?


更新:使用客户端的工作片段https://github.com/docusign/docusign-php-client 工作片段:

$api = new ApiClient( new Configuration );
$api->getOAuth()->setOAuthBasePath( 'account-d.docusign.com' );
$response = $api->requestJWTUserToken(
    '[Integration key]',
    '[User ID]',
    $rsa_key,
    'signature impersonation',
);

标签: phpjwtdocusignapi

解决方案


请考虑使用 SDK,不要尝试自己进行 JWT 编码。它将使它更容易、更安全,并最终为您启用其他功能。开始克隆这个在 PHP 中的 repo,它同时具有 JWT 和 Auth Code Grant 支持。如果您在这里遇到问题 - 请告诉我,我很乐意为您提供帮助。


推荐阅读