Django DocuSign Python SDK-检索新生成的要签名的文档?

Django DocuSign Python SDK-检索新生成的要签名的文档?,django,python-3.x,docusignapi,Django,Python 3.x,Docusignapi,我正在尝试为我的公司开发一个web应用程序,该应用程序将生成一个由一个用户签名的pdf。用户将信息输入表单,然后使用weasyprint,我格式化的html文件生成包含用户信息的pdf。最后,使用docusign的pythonsdk,我想要一个需要签名才能由用户签名的字段。在docusign位工作之前,一切正常 下面是我当前使用的代码,基于@Lee的示例 def Signview(request): myobject = MYOBJECT.objects.filter().order_b

我正在尝试为我的公司开发一个web应用程序,该应用程序将生成一个由一个用户签名的pdf。用户将信息输入表单,然后使用weasyprint,我格式化的html文件生成包含用户信息的pdf。最后,使用docusign的pythonsdk,我想要一个需要签名才能由用户签名的字段。在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异常):

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

以下是两个请求的变量:

oauth\u base\u url:account-d.docusign.com


oauth\u login\u url

我在尝试将docusign与我们的系统集成时遇到了相同的问题。首先,我建议您检查C#版本的docusign文档,它比python版本的文档记录得更好。另外,请查看python sdk repo以查看示例

为了回答您的问题,我将在这里用简化的代码发布我们的实现。在我们的实现中,我们没有实现模板api。我们只需生成自己的pdf文件,并将pdf作为文档附加到信封定义中。此外,还需要将收件人信息(包括选项卡和签名者)和通知信息添加到定义中基本上,信封定义的大多数参数都是可选的,您需要确定需要使用哪些参数。然后,您可以使用信封定义来创建信封

请注意,经过这些过程后,您将获得一个信封id。您可能希望将信封状态更改为“已发送”,以便通过docusign发送。或者,如果您想将其与应用程序集成,可以创建docusign.RecipientViewRequest()以获取临时url,签名者可以使用该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文件数据作为变量文件_数据获取。 然后,请注意函数

docusign_parameter_document() 
在函数中,您需要这一行

base64_doc = base64.b64encode(file_data).decode("utf-8") 
将file_data变量传递到函数中并获取base64编码的数据。最后,您可以将base64_文档附加到对象


已编辑的简短工作示例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)

欢迎来到StackOverflow!请编辑您的问题以添加其他信息:发生了什么?你收到错误消息了吗?(如果是,请附上信息。)谢谢你,@LarryK!老实说,我还没有运行这个代码。我将尽我最大的努力完成这一切,并添加发生了什么的信息。此外,我只是想了解变量是如何使用的。我一直在阅读文档,我觉得我还是不太明白应该是什么。当我运行程序时,在填写完我创建的表单后,会创建特定表单的PDF。我想知道如何使用新生成的PDF进行签名。这就是模板吗?此外,我真的不明白“信封”和“模板”之间的区别是什么,我也不觉得通过我阅读的文档中的措辞可以明显看出。我明白了,这看起来非常有用!我会看一看这个,看看我是否能确定在我的项目中工作的方法。就我的其他尝试而言,我在这里发布了另一个帖子:当然,我还将研究C#实现,因为就python SDK而言,我不太明白我做错了什么。非常感谢您的时间和帮助!:)就实现而言,我确实有几个问题:-标签:这些是必要的吗?从我在参考资料中读到的;“自定义选项卡使帐户可以有一个或多个预配置(自定义)选项卡。自定义选项卡可以在用户标记文档时节省时间,因为用户不必手动设置选项卡的参数。此类别允许以编程方式管理自定义选项卡,包括创建、删除等。”我不太明白,通过阅读本文,为什么它们是必要的。他们到底做什么?-通知/事件通知:这些是必要的吗?在我的工作流程中,用户填写表单,生成PDF,并将其保存到数据库中的模型中。保存后,我试图从数据库中获取该文件,并使用docusign对其进行签名。一次只能有一个用户进行签名,我打算让公司的电子邮件地址接收带有签名文档的电子邮件,因此如果不需要通知,我宁愿不使用它们。此外,对于这些函数中的许多函数,我正计划将它们压缩为一个函数。您好,@JCoder96。很抱歉给你带来了困惑。为了澄清,通知/事件通知是可选的。EventNotification只是充当webhook配置。因此,如果您在服务器上设置了侦听器,则在客户完成签名时会通知它。通知可用于设置合同的到期日和提醒。(在下一篇评论中继续)但是,标签在您的情况下是必要的,因为您需要在pdf上添加一个sign_here标签。整个过程是:1、使用docusign.SignHere()创建一个sign\u here对象;2、使用docusign.Tabs()创建选项卡
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)