首页 > 解决方案 > 如何使用Docusign Python客户端[ApiClient.request_jwt_application_token()]生成的OAuth Applciation令牌?

问题描述

我正在使用python编写一个后端rest应用程序,它从模板创建信封并将前端应用程序的url返回到要显示的创建的信封。

使用Python 客户端,我可以使用以下行创建有效的 OAuth 令牌:

api_client = ApiClient("https://demo.docusign.net/restapi")

resp = api_client.request_jwt_application_token(
    client_id=client_id, # the integrator key
    oauth_host_name=auth_server, # 'account-d.docusign.com'
    private_key_bytes=private_key_file.read(), # private key temp file containing key in bytes
    expires_in=expiration # 3600
)

调用成功,返回如下:

resp
{
    'access_token': 'eyJ0eXAi...81z9D5w',
    'data': None,
    'expires_in': '28800',
    'refresh_token': None,
    'scope': None,
    'token_type': 'Application'
}

并且在客户端中设置了正确的授权标头:

api_client.default_headers
{
    'X-DocuSign-SDK': 'Python', 
    'User-Agent': 'Swagger-Codegen/1.0.0/python', 
    'Authorization': 'Application eyJ0eXAi...81z9D5w'
}

但是,每当我尝试拨打任何电话时,都会收到以下响应:

envelope_api = EnvelopeApi(api_client)
envelope_api.get_envelope(account_id, envelope_id) 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
docusign_esign.client.api_exception.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache', 'Content-Length': '120', 'Content-Type': 'application/json; charset=utf-8', 'X-DocuSign-TraceToken': '14ac...388', 'Date': 'Fri, 20 Dec 2019 17:47:43 GMT', 'Vary': 'Accept-Encoding'})
HTTP response body: b'{"errorCode":"USER_AUTHENTICATION_FAILED","message":"One or both of Username and Password are invalid. invalid_request"}'

是否有额外的步骤来使用此应用程序令牌?Python JWT 示例严重过时,Python 客户端存储库未提及使用新调用所需的任何更改

承诺引入新的应用程序令牌功能

标签: pythonoauthdocusignapi

解决方案


您正在生成一个应用程序令牌,该令牌仅在特定情况下有用,例如与 Datafeeds 一起使用。通常,您希望生成一个用户令牌,其中将包含一个 UserId 作为参数之一。

为此,您需要api_client.configure_jwt_authorization_flow方法。GitHub 上提供了如何使用它的示例:https ://github.com/docusign/eg-01-python-jwt/blob/master/example_base.py


推荐阅读