DocuSign Python SDK-检索新生成的文档进行签名?

时间:2018-07-19 16:46:11

标签: django python-3.x docusignapi

我正在尝试为我的公司开发一个Web应用程序,该应用程序将生成将由一个用户签名的pdf文件。用户将信息输入表单,然后使用weasyprint,我格式化的html文件生成包含用户信息的pdf。最后,使用docusign的python SDK,我想要一个需要签名的字段,以便用户可以签名。直到docusign位起作用为止的一切。

基于@Lee的示例,以下是我当前正在使用的代码。

def Signview(request):
    myobject = MYOBJECT.objects.filter().order_by('-id')[0]
    username = "myUsername"
    integrator_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
    base_url = "https://demo.docusign.net/restapi"
    oauth_base_url = "account-d.docusign.com"
    redirect_uri = "http://localhost:8000/path/to/redirect/uri"
    private_key_filename = "path/to/pKey.txt"
    user_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    client_user_id = 'Your System ID' #This is the actual string I'm using

    # Add a recipient to sign the document
    signer = docusign.Signer()
    signer.email = myobject.email #myobject is a model from my database, containing this information.
    signer.name = myobject.name
    signer.recipient_id = '1'
    signer.client_user_id = client_user_id

    sign_here = docusign.SignHere()
    sign_here.document_id = '1'
    sign_here.recipient_id = '1'
    sign_here.anchor_case_sensitive = 'true'
    sign_here.anchor_horizontal_alignment = 'left'
    sign_here.anchor_ignore_if_not_present = 'false'
    sign_here.anchor_match_whole_word = 'true'

    sign_here.anchor_string = 'Sign Here'
    sign_here.anchor_units = 'cms'
    sign_here.anchor_x_offset = '0'
    sign_here.anchor_y_offset = '0'
    sign_here.tab_label = 'sign_here'
    tabs = docusign.Tabs()
    tabs.sign_here_tabs = [sign_here]

    # Create a signers list, attach tabs to signer, append signer to signers.
    # Attach signers to recipients objects
    signers = []
    tabs = tabs
    signer.tabs = tabs
    signers.append(signer)
    recipients = docusign.Recipients()
    recipients.signers = signers

    # Create an envelope to be signed
    envelope_definition = docusign.EnvelopeDefinition()
    envelope_definition.email_subject = 'My email subject'
    envelope_definition.email_blurb = 'My email blurb.'

    # Add a document to the envelope_definition
    pdfpath = "path/to/mypdf.pdf"
    with open(pdfpath, 'rb') as signfile:
        file_data = signfile.read()
        doc = docusign.Document()
        base64_doc = base64.b64encode(file_data).decode('utf-8')
        doc.document_base64 = base64_doc
        doc.name = "mypdf_signed.pdf"
        doc.document_id = '1'
        envelope_definition.documents = [doc]
        signfile.close()
    envelope_definition.recipients = recipients
    envelope_definition.status = 'sent'

    api_client = docusign.ApiClient(base_url)

    oauth_login_url = api_client.get_jwt_uri(integrator_key, redirect_uri, oauth_base_url)
    print(oauth_login_url)

    api_client.configure_jwt_authorization_flow(private_key_filename, oauth_base_url, integrator_key, user_id, 3600)
    docusign.configuration.api_client = api_client

    auth_api = AuthenticationApi()
    envelopes_api = Envelopes_Api()

    try: #login here via code
        login_infoi = auth_api.login()
        login_accounts = login_info.login_accoutns
        base_url, _ = login_accounts[0].base_url.split('/v2')
        api_client.host = base_url
        docusign.configuration.api_client = api_client

        envelope_summary = envelopes_api.create_envelope(login_accounts[0].account_id, envelope_definition = envelope_definition)

        print(envelope_summary)
    except ApiException as e:
        raise Exception("Exception when calling DocuSign API: %s" % e)
    except Exception as e:
        print(e)

以下是我收到的错误(错误请求API异常):

Environment:


Request Method: GET
Request URL: http://localhost:8000/createquote/genloa/sign/

Django Version: 2.0.6
Python Version: 3.7.0
Installed Applications:
['createquote',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\wkstat\Desktop\Development\LNQuoteTool\createquote\views.py" in Signview
  141.  api_client.configure_jwt_authorization_flow(private_key_filename, oauth_base_url, integrator_key, user_id, 3600)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\api_client.py" in configure_jwt_authorization_flow
  126.                                 post_params=self.sanitize_for_serialization({"assertion": assertion, "grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer"}))

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\api_client.py" in request
  430.                                          body=body)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\rest.py" in POST
  244.                             body=body)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\rest.py" in request
  200.             raise ApiException(http_resp=r)

Exception Type: ApiException at /createquote/genloa/sign/
Exception Value: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'private', 'Content-Type': 'text/html', 'X-AspNetMvc-Version': '5.2', 'X-DocuSign-TraceToken': 'ec891538-ed2f-4250-bc50-966c96065052', 'X-DocuSign-Node': 'DA1DFE4', 'Date': 'Tue, 31 Jul 2018 18:07:22 GMT', 'Content-Length': '11'})
HTTP response body: b'Bad Request'

我已将重定向URI添加到我的docusign管理员帐户中。我不明白我在做什么错。

以下是几个请求的变量:

oauth_base_url :account-d.docusign.com

oauth_login_url https://account-d.docusign.com/oauth/auth?response_type=code&client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&scope=signature%20impersonation&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fcreatequote%2Fgenloa%2Fsign%2F

1 个答案:

答案 0 :(得分:0)

当我尝试将docusign与我们的系统集成时遇到了相同的问题。首先,我建议您检查C#版本的docusign文档,该文档比python更好。 Github Repo。另外,请检查python sdk存储库以查看示例。

为回答您的问题,我将在此处发布带有简体代码的实施。在我们的实现中,我们没有实现模板api。我们只是生成我们自己的pdf文件,并将 PDF 作为文档附加到信封定义。另外,您需要将收件人信息(包括标签和签名者)和通知信息添加到定义中。 基本上,信封定义的大多数参数都是可选的,您需要弄清楚使用它们的必要条件。此后,您可以使用信封定义来创建信封。

请注意,完成这些过程后,您将获得一个信封ID 。您可能需要将信封状态更改为“已发送”以通过docusign发送。或者,如果您想将其与您的应用程序集成,则可以创建一个docusign.RecipientViewRequest()来获取临时URL,签名者可以使用它来打开docusign页面并签署合同。

def create_envelope(file_data, other_info):
    ## .....
    ## file_data is the pdf data
    envelope_definition = create_envelope_definition(file_data, other_info)
    envelope_id = docusign_create_envelope(envelope_definition)
    ## .....

def create_envelope_definition(file_data, other_info):

    envelope_definition = docusign.EnvelopeDefinition()
    envelope_definition.email_subject = 'your email subject'
    envelope_definition.email_blurb = 'your email message here'

    # add a document to the envelope
    doc = docusign_parameter_document(file_data)
    envelope_definition.documents = [doc]

    # Add recipients to sign the document
    recipients = docusign_parameter_recipients(other_info['recipients_info'])

    envelope_definition.recipients = recipients
    envelope_definition.status = 'your init envelope status' # if you want to send it when you create the envelope instantly, set it to sent
    envelope_definition.event_notification = docusign_parameter_event_notification()

    envelope_definition.notification = docusign_parameter_notification(other_info['notification_setting'])

    return envelope_definition

def docusign_parameter_document(file_data):
    doc = docusign.Document()
    base64_doc = base64.b64encode(file_data).decode("utf-8")
    doc.document_base64 = base64_doc
    doc.name = 'your file name'
    doc.document_id = '1'
    return doc

def docusign_parameter_recipients(other_info):
    signers = []
    for e in other_info.contract_signers:
        signer = docusign_parameter_signer(e)
        tabs = docusign_parameter_tabs(other_info, e)
        signer.tabs = tabs
        signers.append(signer)

    recipients = docusign.Recipients()
    recipients.signers = signers
    return recipients


def docusign_parameter_signer(....):
    signer = docusign.Signer()
    signer.email = 'your signer email'
    signer.name = 'your signer name'
    signer.recipient_id = '1' 
    client_user_id = 'your local system id'
    signer.client_user_id = client_user_id
    return signer

def docusign_parameter_tabs(contract_signer):
    sign_here = docusign.SignHere()
    sign_here.document_id = '1'
    sign_here.recipient_id = '1' # notice: here i just simply set up it as 1, after the envelope created, you could retrieve the actual id by using envelopes_api.list_recipients
    sign_here.anchor_case_sensitive = 'true'
    sign_here.anchor_horizontal_alignment = 'left'
    sign_here.anchor_ignore_if_not_present = 'false'
    sign_here.anchor_match_whole_word = 'true'
    sign_here.anchor_string = 'your placeholder string' # you need to use the string to place your sign here tab, otherwise, you need to set up a x,y coordinate to place the tab
    sign_here.anchor_units = 'cms'
    sign_here.anchor_x_offset = '0'
    sign_here.anchor_y_offset = '0'
    sign_here.tab_label = 'sign_here'
    tabs = docusign.Tabs()
    tabs.sign_here_tabs = [sign_here]
    return tabs

def docusign_parameter_event_notification():
    ## return a EventNotification() object

def docusign_parameter_notification(contract):
    ## return a Notification() object

def docusign_create_envelope(envelope_definition):
    ##......some variable declarations here

    api_client = docusign_auth(base_url, integrator_key, redirect_uri, oauth_base_url, private_key_filename, user_id)

    auth_api = AuthenticationApi()
    envelopes_api = EnvelopesApi()

    try:
        ## atuh......
        envelope_summary = envelopes_api.create_envelope(login_accounts[0].account_id,
                                                         envelope_definition=envelope_definition)
        return envelope_summary.envelope_id
    except ApiException as e:
        raise Exception("Exception when calling DocuSign API: %s" % e)

def docusign_auth(base_url, integrator_key, redirect_uri, oauth_base_url, private_key_filename, user_id):
    api_client = docusign.ApiClient(base_url)
    api_client.configure_jwt_authorization_flow(private_key_filename, oauth_base_url, integrator_key, user_id, 3600)
    docusign.configuration.api_client = api_client
    return docusign.configuration.api_client

如有任何疑问,请在下面留下评论。我将尝试解决。希望对您有所帮助。


已编辑 问:如何访问pdf文件并通过docusign发送?

如果您知道pdf文件的路径(包括文件名和扩展名)。然后,您只需要使用以下代码:

with open(filepath, "rb") as fileobj: 
    file_data = fileobj.read() 

获取pdf文件数据作为变量file_data。 然后,请注意该功能

docusign_parameter_document() 

在函数中,您需要此行

base64_doc = base64.b64encode(file_data).decode("utf-8") 

将file_data变量传递到函数中并获取base64编码的数据。最后,您可以将base64_doc附加到Document对象。


已编辑是DEVELOPMENT模式的简短示例

def test(): 
    username = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    integrator_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    # Do not change here if your are using development env
    base_url = "https://demo.docusign.net/restapi"
    # Do not change here if your are using development env
    oauth_base_url = "account-d.docusign.com"
    redirect_uri = "https://xxxxxx.xx"

    # Path to your test key file
    private_key_filename = os.path.dirname(os.path.abspath(__file__)) + "/test_private_key.txt"

    # Same as your username
    user_id = "xxxxxxxxxxxxxxxxxxxxxx"

    # Add a recipient to sign the document
    signer = docusign.Signer()
    signer.email = 'xxxxxxx@xxxxx.com'
    signer.name = 'your name'
    signer.recipient_id = '1'
    client_user_id = '1'
    signer.client_user_id = client_user_id

    # Create a SignHere tab somewhere on the document for the signer to sign
    sign_here = docusign.SignHere()
    sign_here.document_id = '1'
    sign_here.recipient_id = '1'
    sign_here.anchor_case_sensitive = 'true'
    sign_here.anchor_horizontal_alignment = 'left'
    sign_here.anchor_ignore_if_not_present = 'false'
    sign_here.anchor_match_whole_word = 'true'

    ############################################################################## 
    #Please notice that, the string 'Sign Here' need to be shown in your pdf file#
    ############################################################################## 
    sign_here.anchor_string = 'Sign Here'
    sign_here.anchor_units = 'cms'
    sign_here.anchor_x_offset = '0'
    sign_here.anchor_y_offset = '0'
    sign_here.tab_label = 'sign_here'
    tabs = docusign.Tabs()
    tabs.sign_here_tabs = [sign_here]


    # Create a signers list, attach tabs to signer, append signer to singers
    # attach signers to recipients object
    signers = []
    tabs = tabs
    signer.tabs = tabs
    signers.append(signer)
    recipients = docusign.Recipients()
    recipients.signers = signers

    # Create an envelope to be signed
    envelope_definition = docusign.EnvelopeDefinition()
    envelope_definition.email_subject = 'mysubject'
    envelope_definition.email_blurb = 'myblurb'

    # add a document to the envelope_definition
    pdfpath = os.path.dirname(os.path.abspath(__file__)) + "/blank1.pdf"
    with open(pdfpath, 'rb') as signfile:
        file_data = signfile.read()
        doc = docusign.Document()
        base64_doc = base64.b64encode(file_data).decode('utf-8')
        doc.document_base64 = base64_doc
        doc.name = 'myDoc'
        doc.document_id = '1'
        envelope_definition.documents = [doc]
        signfile.close()
    envelope_definition.recipients = recipients
    envelope_definition.status = 'sent'
    cwd = os.getcwd()
    print(cwd)

    api_client = docusign.ApiClient(base_url)

    ### You need open your browser, visit the url onece and approve
    oauth_login_url = api_client.get_jwt_uri(integrator_key, redirect_uri, oauth_base_url)

    api_client.configure_jwt_authorization_flow(private_key_filename, oauth_base_url, integrator_key, user_id, 3600)
    docusign.configuration.api_client = api_client

    auth_api = AuthenticationApi()
    envelopes_api = EnvelopesApi()

    try:
        ############################################
        # Notice: you need login here via your code here
        ############################################
        login_info = auth_api.login()
        login_accounts = login_info.login_accounts
        base_url, _ = login_accounts[0].base_url.split('/v2')
        api_client.host = base_url
        docusign.configuration.api_client = api_client

        envelope_summary = envelopes_api.create_envelope(login_accounts[0].account_id,
                                                         envelope_definition=envelope_definition)
        print(envelope_summary)
    except ApiException as e:
        raise Exception("Exception when calling DocuSign API: %s" % e)
    except Exception as e:
        print(e)