Docusign Django集成-发布上线代码不起作用

时间:2018-10-08 15:22:21

标签: django python-3.x docusignapi

我一直在将Docusign的API与我的代码集成在一起,以使公司的文档签名过程更快。我有一个工作版本(经过许多问题在这里发布和工作了很多小时),并且购买了一个实时版本后,当我尝试调用API时,我的程序不再工作。我相信我已经进行了所有必要的更改,但是我的代码仍然无法正常工作。以下是我的代码:

def Signview(request):
    loa = LOA.objects.filter().order_by('-id')[0] #This is how the document to be signed is retrieved from the database.
    localbillingname = loa.billingname.replace(" ", "_")
    username = "MyUserName"
    integrator_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    base_url = "https://docusign.net/restapi/"
    oauth_base_url = "account.docusign.com/"
    redirect_uri = "http://localhost:8000/path/to/redirectURI" #This has also been set in the admin page for the correct admin key.
    private_key_filename = "path/to/pkey.txt"
    user_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    #client_user_id = 'clientUserID

    # Add a recipient to sign the document
    signer = docusign.Signer()
    signer.email = loa.email
    signer.name = loa.ainame
    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 = 'Signature of individual authorized to act on behalf of customer:'
    sign_here.anchor_units = 'cms'
    sign_here.anchor_x_offset = '.2'
    sign_here.anchor_y_offset = '1.25'
    sign_here.tab_label = 'sign_here'
    sign_here.IgnoreIfNotPresent = True;
    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 = 'Email Subject'
    envelope_definition.email_blurb = 'Email blurb'

    # Add a document to the envelope_definition
    pdfpath = "path/to/pdf.txt"
    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 = "myDocName.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:", oauth_login_url)
    print("oauth_base_url:", 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: #login here via code
            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)
    return HttpResponseRedirect('../success/')

达到这一点时收到以下错误:

Environment:


Request Method: GET
Request URL: http://192.168.109.8:8000/myrequest/url

Django Version: 2.0.6
Python Version: 3.6.5
Installed Applications:
['Home',
 'billing',
 '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 "/home/ag03961/.local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/ag03961/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/ag03961/.local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ag03961/.local/bin/MyServer/MyDjangoApp/views.py" in Signview
  147.         api_client.configure_jwt_authorization_flow(private_key_filename, oauth_base_url, integrator_key, user_id, 3600)

File "/home/ag03961/.local/lib/python3.6/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 "/home/ag03961/.local/lib/python3.6/site-packages/docusign_esign/api_client.py" in request
  430.                                          body=body)

File "/home/ag03961/.local/lib/python3.6/site-packages/docusign_esign/rest.py" in POST
  244.                             body=body)

File "/home/ag03961/.local/lib/python3.6/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': 'no-cache', 'Pragma': 'no-cache', 'Content-Type': 'application/json; charset=utf-8', 'Expires': '-1', 'X-AspNetMvc-Version': '5.2', 'X-DocuSign-TraceToken': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'X-Content-Type-Options': 'nosniff', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block; report=/client-errors/xss', 'X-DocuSign-Node': 'DA2FE100', 'Date': 'Fri, 21 Sep 2018 21:40:55 GMT', 'Content-Length': '25'})
HTTP response body: b'{"error":"invalid_grant"}'

0 个答案:

没有答案