为 DocuSign 创建有效的 JWT 令牌 API
Create a valid JWT Token for DocuSign API
Ι 尝试创建一个有效的 jwt 令牌
从设置中我创建了一个 RSA 密钥对,我得到了没有“-----BEGIN RSA PRIVATE KEY------------END RSA PRIVATE KEY-----”的私钥
“
var rsaPrivateKey = @"MIIEogIBAAKCAQEAoGujdXbYVy68a4CSWz963SpYxVs20/..............HQ/jW8pFom6gJreCDkca5axYo/gXp3W3rQHFTkooTNbOk2MyFMZUqRD3aCG1wuUW3w8TgGX4slrLDV0pP4=";
var jwt = Sign(rsaPrivateKey);
我按照此处的说明进行操作 https://developers.docusign.com/docs/admin-api/admin101/application-auth/ 并在很多小时后创建了此方法
public string Sign(string privateKey)
{
List<string> segments = new List<string>();
var header = new { alg = "RS256", typ = "JWT" };
//For production environments, use account.docusign.com
var payload = new
{
iss = "4f489d61-dc8b------a828-3992e670dcbc",
iat = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds,
aud = "account-d.docusign.com",
scope = "signature impersonation"
};
byte[] headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
byte[] payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload, Formatting.None));
segments.Add(Base64UrlEncode(headerBytes));
segments.Add(Base64UrlEncode(payloadBytes));
string stringToSign = string.Join(".", segments.ToArray());
byte[] bytesToSign = Encoding.UTF8.GetBytes(stringToSign);
byte[] keyBytes = Convert.FromBase64String(privateKey);
var privKeyObj = Asn1Object.FromByteArray(keyBytes);
var privStruct = RsaPrivateKeyStructure.GetInstance((Asn1Sequence)privKeyObj);
ISigner sig = SignerUtilities.GetSigner("SHA256withRSA");
sig.Init(true, new RsaKeyParameters(true, privStruct.Modulus, privStruct.PrivateExponent));
sig.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
byte[] signature = sig.GenerateSignature();
segments.Add(Base64UrlEncode(signature));
return string.Join(".", segments.ToArray());
}
private static string Base64UrlEncode(byte[] input)
{
var output = Convert.ToBase64String(input);
output = output.Split('=')[0]; // Remove any trailing '='s
output = output.Replace('+', '-'); // 62nd char of encoding
output = output.Replace('/', '_'); // 63rd char of encoding
return output;
}
当我检查此工具中的 JWT 验证时 https://jwt.io/#debugger-io,我收到无效签名错误。
我该如何修复令牌??我无法继续步骤 2 获取访问令牌...
很抱歉您在使用 JWT 时遇到问题。我建议您使用 DocuSign C# SDK 而不是尝试编写自己的代码。
然后你可以在这里找到如何使用 JWT 的例子 - https://github.com/docusign/code-examples-csharp。
JWT相关的具体代码在这里——UpdateUserFromJWT()
方法下的https://github.com/docusign/code-examples-csharp/blob/38c2eb46948a3cbf55edcce758f88d775f80cae9/launcher-csharp/Common/RequestItemService.cs
JWT 的常见问题:
- 未获得同意。
- 使用 public 令牌而不是私有令牌。
- 使用格式不正确的令牌。令牌必须与所提供的完全一致,包括 new-lines。
- 请求中没有使用正确的 UserId (GUID)。
- 未在同意中请求“模拟”范围(上面的#1)。
Ι 尝试创建一个有效的 jwt 令牌
从设置中我创建了一个 RSA 密钥对,我得到了没有“-----BEGIN RSA PRIVATE KEY------------END RSA PRIVATE KEY-----”的私钥 “
var rsaPrivateKey = @"MIIEogIBAAKCAQEAoGujdXbYVy68a4CSWz963SpYxVs20/..............HQ/jW8pFom6gJreCDkca5axYo/gXp3W3rQHFTkooTNbOk2MyFMZUqRD3aCG1wuUW3w8TgGX4slrLDV0pP4=";
var jwt = Sign(rsaPrivateKey);
我按照此处的说明进行操作 https://developers.docusign.com/docs/admin-api/admin101/application-auth/ 并在很多小时后创建了此方法
public string Sign(string privateKey)
{
List<string> segments = new List<string>();
var header = new { alg = "RS256", typ = "JWT" };
//For production environments, use account.docusign.com
var payload = new
{
iss = "4f489d61-dc8b------a828-3992e670dcbc",
iat = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds,
aud = "account-d.docusign.com",
scope = "signature impersonation"
};
byte[] headerBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(header, Formatting.None));
byte[] payloadBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload, Formatting.None));
segments.Add(Base64UrlEncode(headerBytes));
segments.Add(Base64UrlEncode(payloadBytes));
string stringToSign = string.Join(".", segments.ToArray());
byte[] bytesToSign = Encoding.UTF8.GetBytes(stringToSign);
byte[] keyBytes = Convert.FromBase64String(privateKey);
var privKeyObj = Asn1Object.FromByteArray(keyBytes);
var privStruct = RsaPrivateKeyStructure.GetInstance((Asn1Sequence)privKeyObj);
ISigner sig = SignerUtilities.GetSigner("SHA256withRSA");
sig.Init(true, new RsaKeyParameters(true, privStruct.Modulus, privStruct.PrivateExponent));
sig.BlockUpdate(bytesToSign, 0, bytesToSign.Length);
byte[] signature = sig.GenerateSignature();
segments.Add(Base64UrlEncode(signature));
return string.Join(".", segments.ToArray());
}
private static string Base64UrlEncode(byte[] input)
{
var output = Convert.ToBase64String(input);
output = output.Split('=')[0]; // Remove any trailing '='s
output = output.Replace('+', '-'); // 62nd char of encoding
output = output.Replace('/', '_'); // 63rd char of encoding
return output;
}
当我检查此工具中的 JWT 验证时 https://jwt.io/#debugger-io,我收到无效签名错误。
我该如何修复令牌??我无法继续步骤 2 获取访问令牌...
很抱歉您在使用 JWT 时遇到问题。我建议您使用 DocuSign C# SDK 而不是尝试编写自己的代码。
然后你可以在这里找到如何使用 JWT 的例子 - https://github.com/docusign/code-examples-csharp。
JWT相关的具体代码在这里——UpdateUserFromJWT()
方法下的https://github.com/docusign/code-examples-csharp/blob/38c2eb46948a3cbf55edcce758f88d775f80cae9/launcher-csharp/Common/RequestItemService.cs
JWT 的常见问题:
- 未获得同意。
- 使用 public 令牌而不是私有令牌。
- 使用格式不正确的令牌。令牌必须与所提供的完全一致,包括 new-lines。
- 请求中没有使用正确的 UserId (GUID)。
- 未在同意中请求“模拟”范围(上面的#1)。