1+ from os import path
2+ import sys
3+ import subprocess
4+
5+ from docusign_esign import ApiClient
6+ from docusign_esign .client .api_exception import ApiException
7+ from app .jwt_helpers import get_jwt_token , get_private_key
8+ from app .eSignature .examples .eg002_signing_via_email import Eg002SigningViaEmailController
9+ from jwt_config import DS_JWT
10+
11+ # pip install DocuSign SDK
12+ subprocess .check_call ([sys .executable , '-m' , 'pip' , 'install' , 'docusign_esign' ])
13+
14+ SCOPES = [
15+ "signature" , "impersonation"
16+ ]
17+
18+ def get_consent_url ():
19+ url_scopes = "+" .join (SCOPES )
20+
21+ # Construct consent URL
22+ redirect_uri = "https://developers.docusign.com/platform/auth/consent"
23+ consent_url = f"https://{ DS_JWT ['authorization_server' ]} /oauth/auth?response_type=code&" \
24+ f"scope={ url_scopes } &client_id={ DS_JWT ['ds_client_id' ]} &redirect_uri={ redirect_uri } "
25+
26+ return consent_url
27+
28+ def get_token (private_key , api_client ):
29+
30+ # Call request_jwt_user_token method
31+ token_response = get_jwt_token (private_key , SCOPES , DS_JWT ["authorization_server" ], DS_JWT ["ds_client_id" ], DS_JWT ["ds_impersonated_user_id" ])
32+ access_token = token_response .access_token
33+
34+ # Save API account ID
35+ user_info = api_client .get_user_info (access_token )
36+ accounts = user_info .get_accounts ()
37+ api_account_id = accounts [0 ].account_id
38+ base_path = accounts [0 ].base_uri + "/restapi"
39+
40+ return {"access_token" : access_token , "api_account_id" : api_account_id , "base_path" : base_path }
41+
42+ def get_args (api_account_id , access_token , base_path ):
43+ signer_email = input ("Please enter the signer's email address: " )
44+ signer_name = input ("Please enter the signer's name: " )
45+ cc_email = input ("Please enter the cc email address: " )
46+ cc_name = input ("Please enter the cc name: " )
47+
48+ envelope_args = {
49+ "signer_email" : signer_email ,
50+ "signer_name" : signer_name ,
51+ "cc_email" : cc_email ,
52+ "cc_name" : cc_name ,
53+ "status" : "sent" ,
54+ }
55+ args = {
56+ "account_id" : api_account_id ,
57+ "base_path" : base_path ,
58+ "access_token" : access_token ,
59+ "envelope_args" : envelope_args
60+ }
61+
62+ return args
63+
64+ def run_example (private_key , api_client ):
65+ jwt_values = get_token (private_key , api_client )
66+ args = get_args (jwt_values ["api_account_id" ], jwt_values ["access_token" ], jwt_values ["base_path" ])
67+ envelope_id = Eg002SigningViaEmailController .worker (args , DS_JWT ["doc_docx" ], DS_JWT ["doc_pdf" ])
68+ print ("Your envelope has been sent." )
69+ print (envelope_id )
70+
71+ def main ():
72+
73+ api_client = ApiClient ()
74+ api_client .set_base_path (DS_JWT ["authorization_server" ])
75+ api_client .set_oauth_host_name (DS_JWT ["authorization_server" ])
76+
77+ private_key = get_private_key (DS_JWT ["private_key_file" ]).encode ("ascii" ).decode ("utf-8" )
78+
79+ try :
80+ run_example (private_key , api_client )
81+ except ApiException as err :
82+ body = err .body .decode ('utf8' )
83+
84+ if "consent_required" in body :
85+ consent_url = get_consent_url ()
86+ print ("Open the following URL in your browser to grant consent to the application:" )
87+ print (consent_url )
88+ consent_granted = input ("Consent granted? Select one of the following: \n 1)Yes \n 2)No \n " )
89+ if consent_granted == "1" :
90+ run_example (private_key , api_client )
91+ else :
92+ sys .exit ("Please grant consent" )
93+
94+ else :
95+ process_error (err )
96+
97+
98+ main ()
0 commit comments