DocuSign – Send documents on Behalf Of Other Users

Quite a pain in the neck to implement this feature. DocuSign has published the SOBO implementation on this link, but unfortunately this would only work on the demo site. Reasons explained below, but just trying to get some working sample code in. The following snippet is how the DocuSign link suggests things should work, but it worked only on Demo, not on Production. Look at the comments below the code for production implementation.

from access import *
import sys, json, urllib, urllib2
import base64

account_id = 'a71d1fc7-xxxx-yyyy-zzzz-eeb0e6a2aa60'
docusign_base_url = 'https://demo.docusign.net/restapi/v2' # or 'https://na2.docusign.net/restapi/v2' for production
api_user = 'apiuser@email.com'
api_user_password = 'docuSignPassword'
api_user_access_token = 'xhasfY&AsdGfLy/NhLcWSmS8ug='
integratorKey = "ACCT-54410dda-31c0-0000-1111-5013abcd20fb"

operating_user = 'varun.verma@email.com'
authenticateStr = "" \
                    "" + username + "" \
                    "" + password + "" \
                    "" + integratorKey + "" \
                    ""

# ------------------------------------------------------------------------
# STEP 1 - Get OAuth Token
# ------------------------------------------------------------------------
url = '%s/oauth2/token' % (docusign_base_url)
print 'Get OAuth Token URL:%s' % (url)
body = 'grant_type=password&client_id=%s&username=%s&password=%s&scope=api' % (integratorKey, username, password)
headers = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length' : len(body)}
print body
print headers
req = urllib2.Request(url, body, headers=headers)
print req
print req.headers
response = urllib2.urlopen(req)
json_response = response.read()
print json_response

status = response.code
if (status != 200):
    print("Error calling webservice, status is: %s" % status); sys.exit()

access_token = json.loads(json_response)['access_token']
print 'API User Access Token: %s' % access_token

# ------------------------------------------------------------------------
# STEP 2 - Obtain access token for the operating user
# ------------------------------------------------------------------------
authorization = 'bearer %s' % (access_token)
url = '%s/oauth2/token' % (docusign_base_url)
body = 'grant_type=password&client_id=%s&username=%s&password=%s&scope=api' % (integratorKey, operating_user, password)
headers = {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length' : len(body), 'Authorization' : authorization}
req = urllib2.Request(url, body, headers=headers)
response = urllib2.urlopen(req)
json_response = response.read()
print json_response

status = response.code
if (status != 200):
    print("Error calling webservice, status is: %s" % status); sys.exit()

access_token = json.loads(json_response)['access_token']
print 'Operating User Access Token: %s' % (access_token)

# ------------------------------------------------------------------------
# STEP 3 - Send signature request on behalf of operating user
# ------------------------------------------------------------------------
f = open(document_name, 'rb')
file_contents = f.read()
f.close()

b64_content = base64.b64encode(file_contents)
dict_envelope = {} # Enter your DocuSign envelope here. Skipping for brevity
requestBody =  "\r\n" + \
                "\r\n" + \
                "--myboundary\r\n" + \
                "Content-Type: application/json\r\n" + \
                "Content-Disposition: form-data\r\n" + \
                "\r\n" + \
                json.dumps(dict_envelope)+"\r\n" + \
                "--myboundary--\r\n" + \
                "\r\n"

authorization = 'bearer %s' % (access_token)
headers = { 'Authorization' : authorization,
            'Accept' : 'application/json',
            'Content-Type' : 'multipart/form-data;boundary=myboundary',
            'Content-Length' : len(requestBody)
            }

url = '%s/accounts/%s/envelopes' % (docusign_base_url, account_id)
req = urllib2.Request(url, data=requestBody, headers=headers)
print "Before request"
response = urllib2.urlopen(req)
print "Response"
json_response = response.read()
print json_response
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s