无法使用dcousign rest API下载完整的信封pdf

时间:2014-09-19 20:39:32

标签: docusignapi

我正在尝试使用REST API访问签名信封,并且我从REST API获得响应,但是当我将响应保存到pdf时,它不会显示任何内容。

下面是我为实现此目的而编写的一个小python代码。

import json
import requests
url = "https://demo.docusign.net/restapi/v2/accounts/[Account ID]/envelopes/[Envelope ID]/documents/1"
headers = {'X-DocuSign-Authentication': '<DocuSignCredentials><Username>[username]</Username><Password>[password]</Password><IntegratorKey>[Integrator Key]</IntegratorKey></DocuSignCredentials>'}
Response = requests.get(url, headers = headers)
file = open("agreement.pdf", "w")
file.write(Response.text.encode('UTF-8'))
file.close()

2 个答案:

答案 0 :(得分:0)

尝试使用httplib2,内容总是会通过DocuSign以PDF格式返回,因此我认为您不需要设置编码。

import json
import requests
import httplib2

url = "https://demo.docusign.net/restapi/v2/accounts/[Account ID]/envelopes/[Envelope ID]/documents/1"
headers = {'X-DocuSign-Authentication': '<DocuSignCredentials><Username>[username]</Username><Password>[password]</Password><IntegratorKey>[Integrator Key]</IntegratorKey></DocuSignCredentials>'}
http = httplib2.Http();
response, content = http.request(url, 'GET', headers = headers)
status = response.get('status');
if (status != '200'):
  print("Error: " % status); sys.exit();
file = open("agreement.pdf", "w")
file.write(content)
file.close()

答案 1 :(得分:0)

您是否看过DocuSign API演练?这些可以通过开发人员中心访问,我建议你看一下9个常见API用例的代码示例,每个用例用Python(和其他5种语言)编写,包括第6个演练,向您展示如何查询和下载已完成信封的文档。

您的代码的主要问题是它假设响应仅包含文档数据,这是不正确的。响应将具有标题和正文,这就是当您将文档全部写入文件时无法呈现文档的原因。

我建议你使用httplib2,这是以下Python示例使用的内容: http://iodocs.docusign.com/APIWalkthrough/getEnvelopeDocuments

以下是Python代码的片段,但请查看上面的完整代码链接:

#
# STEP 2 - Get Envelope Document(s) Info and Download Documents
#

# append envelopeUri to baseURL and use in the request
url = baseUrl + envelopeUri + "/documents";
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();
data = json.loads(content);

envelopeDocs = data.get('envelopeDocuments');
uriList = [];
for docs in envelopeDocs:
    # print document info
    uriList.append(docs.get("uri"));
    print("Document Name = %s, Uri = %s" % (docs.get("name"), uriList[len(uriList)-1]));

    # download each document
    url = baseUrl + uriList[len(uriList)-1];
    headers = {'X-DocuSign-Authentication': authenticateStr};
    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();
    fileName = "doc_" + str(len(uriList)) + ".pdf";
    file = open(fileName, 'w');
    file.write(content);
    file.close();

print ("\nDone downloading document(s)!\n");