MaxRetryError with Docusign in Django

2019-07-28 17:47发布

I am working on a webapp for my company. After the user enters information into the form that I provide, a pdf form is generated (using weasyprint), filling in the customer's information into the fields necessary. However, I am needing to integrate docusign's webapi into my project. I have messed around with the code a bit, and have gotten to the current point below:

def Signview(request):

    username = "myDocusignUsername"
    integrator_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    base_url = "https://demo.docusign.net/restapi"
    oauth_base_url = "account-d.docusign.com/oauth/auth?request_type=code&scope=signature&client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&redirect_uri=http://localhost:8000/page/to/redirect/to"
    redirect_uri = "http://localhost:8000/page/to/redirect/to"
    private_key_filename = "createquote/keys/pKey.txt"
    user_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

    file_contents = open("path/to/mydoc.pdf", 'rb').read()

    envelope_definition = docusign.EnvelopeDefinition()
    envelope_definition.email_subject = 'My email subject'
    envelope_definition.email_blurb = 'My email blurb'

    doc = docusign.Document()
    base64_doc = base64.b64encode(file_contents).decode('utf-8')
    doc.document_base64 = base64_doc
    doc.name = "mydoc.pdf"
    doc.document_id = '1'
    envelope_definition.documents = [doc]

    signer = docusign.Signer()
    signer.email = loa.email
    signer.name = loa.ainame
    signer.recipient_id = '1'

    sign_here = docusign.SignHere()
    sign_here.document_id = '1'
    sign_here.page_number = '1'
    sign_here.recipient_id = '1'
    sign_here.x_position = '100'
    sign_here.y_position = '100'
    sign_here.scale_value = '0.5'

    tabs = docusign.Tabs()
    tabs.sign_here_tabs = [sign_here]
    signer.tabs = tabs

    recipients = docusign.Recipients()
    recipients.signers = [signer]
    envelope_definition.recipients = recipients

    envelope_definition.status = 'sent'

    auth_api = AuthenticationApi()
    envelopes_api = EnvelopesApi()

    try:
        login_info = auth_api.login(api_password='true', include_account_id_guid='true')
        assert login_info is not None
        assert len(login_info.login_accounts) > 0
        login_accounts = login_info.login_accounts
        assert login_accounts[0].account_id is not None

        base_url, _ = login_accounts[0].base_url.split('/v2')
        self.api_client.host = base_url
        docusign.configuration.api_client = self.api_client

        envelope_summary = envelopes_api.create_envelope(login_accounts[0].account_id, envelope_definition=envelope_definition)
        assert envelope_summary is not None
        assert envelope_summary.envelope_id is not None
        assert envelope_summary.status == 'sent'

        print("EnvelopeSummary: ", end="")
        pprint(envelope_summary)

    except ApiException as e:
        print("\nException when calling DocuSign API: %s" % e)
        assert e is None # make the test case fail in case of an API exception

    return HttpResponse({'sent'})

This being based on the code located here: https://github.com/docusign/docusign-python-client/blob/master/test/unit_tests.py , Lines 129 through 194.

What I am trying to do is have the document that was generated (and saved to the models in the database I have) sent in an email (automatically) containing the form to be signed using Docusign's Python SDK. However, when I run this code and reach this particular view, I get the following error:

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\urllib3\connection.py" in _new_conn
  171.                 (self._dns_host, self.port), self.timeout, **extra_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\util\connection.py" in create_connection
  56.     for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\socket.py" in getaddrinfo
  748.     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):

During handling of the above exception ([Errno 11004] getaddrinfo failed), another exception occurred:

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  600.                                                   chunked=chunked)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in _make_request
  343.             self._validate_conn(conn)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in _validate_conn
  849.             conn.connect()

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connection.py" in connect
  314.         conn = self._new_conn()

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connection.py" in _new_conn
  180.                 self, "Failed to establish a new connection: %s" % e)

During handling of the above exception (<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000678CD30>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed), another exception occurred:

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
  125.      login_info = auth_api.login(api_password='true', include_account_id_guid='true')

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\apis\authentication_api.py" in login
  388.             (data) = self.login_with_http_info(**kwargs)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\apis\authentication_api.py" in login_with_http_info
  472.                                         collection_formats=collection_formats)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\api_client.py" in call_api
  385.                                    _return_http_data_only, collection_formats, _preload_content, _request_timeout)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\api_client.py" in __call_api
  208.                                      _request_timeout=_request_timeout)

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

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\rest.py" in GET
  209.                             query_params=query_params)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\docusign_esign\rest.py" in request
  188.                                               headers=headers)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\request.py" in request
  68.                                            **urlopen_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\request.py" in request_encode_url
  89.         return self.urlopen(method, url, **extra_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\poolmanager.py" in urlopen
  322.             response = conn.urlopen(method, u.request_uri, **kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  667.                                 **response_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  667.                                 **response_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  667.                                 **response_kw)

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\connectionpool.py" in urlopen
  638.                                         _stacktrace=sys.exc_info()[2])

File "C:\users\wkstat\appdata\local\programs\python\python37\lib\site-packages\urllib3\util\retry.py" in increment
  398.             raise MaxRetryError(_pool, url, error or ResponseError(cause))

Exception Type: MaxRetryError at /createquote/genloa/sign/
Exception Value: HTTPSConnectionPool(host='www.docusign_esign.net', port=443): Max retries exceeded with url: /restapi/v2/login_information?api_password=true&include_account_id_guid=true (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000678CD30>: Failed to establish a new connection: [Errno 11004] getaddrinfo failed'))

Does anyone know what I am doing wrong here? I really am lost, and Docusign doesn't provide guidance for the WebAPI, from what I've been told when I called them.

Thank you in advance!

1条回答
家丑人穷心不美
2楼-- · 2019-07-28 18:25

This was recently opened and resolved for the python API (https://github.com/docusign/docusign-python-client/issues/15), but the change has not been released yet.

Either way, since you want to use the demo URL, you will need to override it like you are doing above. The specific problem you are having is that the AuthenticationApi using the wrong url. You need to pass it the api client:

auth_api = AuthenticationApi(api_client=api_client)
查看更多
登录 后发表回答