尝试使用DocuSign API时与RSA密钥相关的错误

问题描述

我正在尝试在DocuSign API中使用“ api_client.request_jwt_user_token”。但是我不确定如何转换RSA PEM文件

import rsa
from docusign_esign import apiclient

with open('docusign.pem',mode='rb') as privatefile:
    keydata = privatefile.read()
privkey = rsa.PrivateKey.load_pkcs1(keydata)
key1 = privkey.save_pkcs1(format='DER')

api_client = apiclient()
api_client.host = 'https://demo.docusign.net/restapi'
access_token = 'eyJ0eXAiOiJKV...etc'
api_client.set_default_header('Authorization','Bearer ' +
                              access_token)
client_id = '2e65...etc'
user_id = 12341234
oauth_host_name = 'https://demo.docusign.net/restapi'
private_key_bytes = key1
expires_in = 28800
result = api_client.request_jwt_user_token(client_id,user_id,oauth_host_name,private_key_bytes,expires_in)

我在API调用中收到一个Python异常:

Could not deserialize key data.

这是在“ site-packages / cryptography / hazmat / backends / openssl / backend.py”中

我猜想有一种更简单的方法可以将RSA PEM文件导入API。

参考:https://pypi.org/project/rsa/

更新

我还尝试使用openssl转换PEM文件,然后改为读取它:

(命令提示符)

$ openssl rsa -inform pem -in docusign.pem -outform der -out docusign.der

结果文件为1192字节。

代码

with open('docusign.der',mode='rb') as privatefile:
    key1 = privatefile.read()

这会导致相同的错误

更新2

我尝试了在键上没有进行任何转换以及设置不同的问题,现在我得到了另一个错误

oauth_host_name = 'account-d.docusign.com'

返回错误的HTTP:

Exception Value: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache','Pragma': 'no-cache','Content-Type': 'application/json; 
charset=utf-8','Expires': '-1','Server': 'Microsoft-IIS/8.5','X-AspNetMvc-Version': '5.2','X-DocuSign-Tracetoken':
'666633df-ac84-4193-a282-e81075302413','X-Content-Type-Options': 
'nosniff','Strict-Transport-Security': 'max-age=31536000; 
includeSubDomains; preload,max-age=15768000','x-frame-options': 
'SAMEORIGIN','X-XSS-Protection': '1; mode=block;
report=/client-errors/xss','X-DocuSign-Node': 'SE1DFE2','Date':
'Sat,22 Aug 2020 18:53:16 GMT','Content-Length': '27'})
HTTP response body: b'{"error":"invalid_request"}'

关于出了什么问题似乎没有多少线索。

解决方法

这时,您可以从DocuSign eSignature设置工具(以前称为eSignature管理工具)获得RSA私钥。使用“集成/ API和密钥”屏幕。

DocuSign具有增强请求ID-4714,它将使开发人员能够将其密钥对的公钥上载到DocuSign。但是,这一次您从DocuSign 下载私钥来创建JWT。

已添加

不幸的是,request_jwt_user_token方法仅记录在源文件中。这是我对您调用该方法的评论:

  • 对于private_key_bytes属性,将下载的私钥用作简单的ASCII字符串(带有新行)。换句话说,它的值应该类似于

此:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAkbz3bi31zrH2ry4p8S4ncPoMdkUyu+MG46m9BalOKzWNNAvW
1LVs5ftlXxzA6V0m6nx895w8S761/qZ8xtAAl99DezRn/3CueeBUyw+tvlmEBu1C
IJK69GVoSInIKf6qyeL1WxxFV5R17QtIiQeT2yCa/fitCaxwxkNlYpP4wd5tcG0W
pNHUqbQmWywTRMIBa6yYMc9qvDTJpOubrTal3BI8VqmBww0diFWLm4l6+IZ7QvDV
Q48JYErfywNXyYTxZ4kugQcuNDR3vSuwR3KSlmiRcfhkn+2jb5rx6kKnZjXQLhg3
...
PEHgznlGh/vUboCuA4tQOcKytxFfKG4F+jM/g4GH9z46KZOow3Hb6g==
-----END RSA PRIVATE KEY-----
  • JWT生产的访问令牌的最大寿命为1小时,因此请使用3600作为expires_in
  • user_id是要被冒用的使用者的 guid 。从“用户”部分的“电子签名”设置工具或“ API和密钥”屏幕中获取您自己的值。
  • 您需要指定scopes参数:使用signature