【问题标题】:Generate JSON Web Token (RS256) to access DocuSign using Google Apps Script使用 Google Apps 脚本生成 JSON Web 令牌 (RS256) 以访问 DocuSign
【发布时间】:2021-11-07 00:32:51
【问题描述】:

我正在尝试仅使用 Apps 脚本从 Docusign 发送信封。

function createJWT(){
  const header = {
    alg: 'RS256',
    typ: 'JWT',
  };

  const now = Date.now();
  const expires = new Date(now);
  expires.setHours(expires.getHours() + 1);
  const payload = {
    exp: Math.round(expires.getTime() / 1000),
    iat: Math.round(now / 1000),
    iss: "integrator key",
    sub: "user id",
    aud: "url",
    scope: "scopes"
  };

  var toSign = Utilities.base64EncodeWebSafe(JSON.stringify(header)) + '.' + Utilities.base64EncodeWebSafe(JSON.stringify(payload));
  toSign = toSign.replace(/=+$/, '');

  var privateKey = "-----BEGIN RSA PRIVATE KEY-----<private key here>-----END RSA PRIVATE KEY-----";

  const signatureBytes = Utilities.computeRsaSha256Signature(
    toSign,
    privateKey
  );
  const signature = Utilities.base64EncodeWebSafe(signatureBytes);

  return toSign + '.' + signature;
}

Utilities.computeRsaSha256Signature() 返回:

异常:无效参数:键

如何使用 RSA 密钥对 创建 JWT?
来自 Docusign 的公钥/私钥:
-----BEGIN PUBLIC KEY-----\n{这里是公钥}\n-----END PUBLIC KEY----
------BEGIN RSA PRIVATE KEY-----\n{这里是私钥}\n-----END RSA PRIVATE KEY-----

【问题讨论】:

    标签: javascript google-apps-script jwt docusignapi rsa-sha256


    【解决方案1】:

    使用Utilities.base64Encode() 而不是Utilities.base64EncodeWebSafe()

    完成替换后,删除带有toSign.replace(...) 的行,然后就可以开始使用了。


    更新

    上述修复仍然适用,但我想我知道您的核心问题是什么。查看this SO thread

    Utilities.computeRsaSha256Signature() 需要一个以 BEGIN PRIVATE KEY 而不是 BEGIN RSA PRIVATE KEY 开头的私钥。您需要找到一个与 Google Apps 脚本兼容的第三方库,该库可以计算第二种形式 (PKCS#1) 的密钥。

    【讨论】:

    • 是的,它解决了我的问题。我使用 openSSL 将私钥转换为以下行:openssl pkcs8 -topk8 -inform pem -in private.pem -outform pem -nocrypt -out newPrivate.pem
    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2019-10-15
    相关资源
    最近更新 更多