DocuSign JWT 响应错误 invalid_request 使用 PHP
DocuSign JWT response error invalid_request using 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',
);
请考虑使用 SDK 而不要尝试自己进行 JWT 编码。
它将使它更容易、更安全,并最终为您启用其他功能。
开始克隆 this repo,它在 PHP 中,同时支持 JWT 和 Auth Code Grant。如果您在这里遇到问题 - 请告诉我,我们很乐意提供帮助。
我相信你想替换你的台词:
$header = urlencode( base64_encode( json_encode( $header ) ) );
$body = urlencode( base64_encode( json_encode( $body ) ) );
和
$header = str_replace('=', '', strtr(base64_encode($header ), '+/', '-_'));
$body = str_replace('=', '', strtr(base64_encode($body), '+/', '-_'));
与您的签名相同:
$signature = str_replace('=', '', strtr(base64_encode($signature), '+/', '-_'));
我从 jwtphpjquery 中提取了这个想法。
我在尝试从 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',
);
请考虑使用 SDK 而不要尝试自己进行 JWT 编码。 它将使它更容易、更安全,并最终为您启用其他功能。 开始克隆 this repo,它在 PHP 中,同时支持 JWT 和 Auth Code Grant。如果您在这里遇到问题 - 请告诉我,我们很乐意提供帮助。
我相信你想替换你的台词:
$header = urlencode( base64_encode( json_encode( $header ) ) );
$body = urlencode( base64_encode( json_encode( $body ) ) );
和
$header = str_replace('=', '', strtr(base64_encode($header ), '+/', '-_'));
$body = str_replace('=', '', strtr(base64_encode($body), '+/', '-_'));
与您的签名相同:
$signature = str_replace('=', '', strtr(base64_encode($signature), '+/', '-_'));
我从 jwtphpjquery 中提取了这个想法。