【发布时间】:2018-08-01 03:51:26
【问题描述】:
我正在尝试为我的公司开发一个 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
【问题讨论】:
-
欢迎来到 StackOverflow!请编辑您的问题以添加其他信息:发生了什么?你收到错误信息了吗? (如果有,请附上信息。)
-
谢谢你,@LarryK!说实话,我还没有运行这段代码。我会尽我所能完成整理,并添加有关发生情况的信息。此外,我只是想了解如何使用这些变量。我一直在阅读文档,但我觉得我仍然不明白应该是什么。当我运行程序时,在填写我创建的表单后,会创建一个特定表单的 PDF。我想知道如何使用新生成的 PDF 进行签名。这就是模板吗?
-
此外,我只是真的不明白“信封”和“模板”之间的区别是什么,而且我觉得通过我所写的文档中的措辞并不明显阅读。
标签: django python-3.x docusignapi