DocuSign API发送Evnelope Python示例错误

时间:2016-06-30 22:49:37

标签: python docusignapi

我正在使用DocuSign给出的示例,通过Python使用REST API发送信封。我几乎复制+粘贴代码,并分配了用户名,密码,收件人信息等(由于显而易见的原因,它不在代码块中)。我的问题是我收到了INVALID_REQUEST_BODY错误,特别是:

The request body is missing or improperly formatted. Additional text encountered after finished reading JSON content: -. Path '', line 3, position 1.

这是代码:

 import httplib2, json, sys


authenticateStr = "<DocuSignCredentials>" \
          "<Username>" + username + "</Username>" \
                                    "<Password>" + password + "</Password>" \
                                                              "<IntegratorKey>" + integratorKey + "</IntegratorKey>" \
                                                                                                  "</DocuSignCredentials>";


#
# STEP 1 - Login
#
url = 'https://demo.docusign.net/restapi/v2/login_information';
headers = {'X-DocuSign-Authentication': authenticateStr, 'Accept': 'application/json'};
http = httplib2.Http();
response, content = http.request(url, 'GET', headers=headers);

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

# get the baseUrl and accountId from the response body
data = json.loads(content);
loginInfo = data.get('loginAccounts');
D = loginInfo[0];
baseUrl = D['baseUrl'];
accountId = D['accountId'];

envelopeDef = "{\"emailBlurb\":\"This comes from Python\"," + \
              "\"emailSubject\":\"API Call for adding signature request to document and sending\"," + \
              "\"documents\":[{" + \
              "\"documentId\":\"1\"," + \
              "\"name\":\"test_doc.txt\"}]," + \
              "\"recipients\":{" + \
              "\"signers\":[{" + \
              "\"email\":\"" + signer + "\"," + \
              "\"name\":\"Name\"," + \
              "\"recipientId\":\"1\"," + \
              "\"tabs\":{" + \
              "\"signHereTabs\":[{" + \
              "\"xPosition\":\"100\"," + \
              "\"yPosition\":\"100\"," + \
              "\"documentId\":\"1\"," + \
              "\"pageNumber\":\"1\"" + "}]}}]}," + \
              "\"status\":\"sent\"}";

# convert the file into a string and add to the request body
fileContents = open("test_doc.txt", "r").read();

requestBody = "\r\n\r\n--BOUNDARY\r\n" + \
              "Content-Type: application/json\r\n" + \
              "Content-Disposition: form-data\r\n" + \
              "\r\n" + \
              envelopeDef + "\r\n\r\n--BOUNDARY\Dr\n" + \
              "Content-Type: text/plain\r\n" + \
              "Content-Disposition: file; filename=\"test_doc.txt\"; documentId=1\r\n" + \
              "\r\n" + \
              fileContents + "\r\n" + \
              "--BOUNDARY--\r\n\r\n";

# append "/envelopes" to the baseUrl and use in the request
url = baseUrl + "/envelopes";
headers = {'X-DocuSign-Authentication': authenticateStr, 'Content-Type': 'multipart/form-data; boundary=BOUNDARY',
           'Accept': 'application/json'};
http = httplib2.Http();
response, content = http.request(url, 'POST', headers=headers, body=requestBody);
status = response.get('status');
if (status != '201'):
    print("Error calling webservice, status is: %s\nError description - %s" % (status, content));
    sys.exit();
data = json.loads(content);
envId = data.get('envelopeId');

1 个答案:

答案 0 :(得分:3)

这一行:

envelopeDef + "\r\n\r\n--BOUNDARY\Dr\n" + \

其中有一个额外的'D'字符。删除它工作。