簡體   English   中英

DocuSign API-JavaScript

[英]DocuSign API - JavaScript

下午! 我是JavaScript和DocuSign API的新手。 我嘗試按照示例進行操作,但是遇到了ReferenceError。

為了進行測試,我在PC上文件夾的根目錄中有async.js,fs.js,request.js和require.js以及測試HTML文檔

HTML:

<html>
<head>
        <script src="require.js"></script>
        <script src="example.js"></script>

</head>

<body>
<div id="message"></div>
</body>

</html>

我收到以下錯誤:ReferenceError:未定義async async.waterfall(

以下是我從DocuSign網站獲得的example.js代碼,根據require函數周圍的另一個錯誤對它進行了調整:

// Request Signature on a Document (JavaScript)

// To run this sample
//  1. Copy the file to your local machine and give .js extension (i.e. example.js)
//  2. Change "***" to appropriate values
//  3. Install async and request packages
//     npm install async
//     npm install request
//     npm install fs
//  4. execute
//     node example.js 
//

//var     async = require("async"),     // async module
//        request = require("request"),     // request module
//      fs = require("fs");         // fs module

require(["request"], function (request){});
require(["async"], function (async){});
require(["fs"], function (fs){});


var     email = "email@email",              // your account email
        password = "apassword",         // your account password
        integratorKey = "akey",         // your Integrator Key 

(found on the Preferences -> API page)
    recipientName = "Bob",          // recipient (signer) name
        documentName = "afile",         // copy document with this name into same 

directory!
        baseUrl = "";               // we will retrieve this through the Login call

async.waterfall(
  [
    /////////////////////////////////////////////////////////////////////////////////////
    // Step 1: Login (used to retrieve your accountId and baseUrl)
    /////////////////////////////////////////////////////////////////////////////////////
function(next) {
        var url = "https://demo.docusign.net/restapi/v2/login_information";
        var body = "";  // no request body for login api call

        // set request url, method, body, and headers
        var options = initializeRequest(url, "GET", body, email, password);

        // send the request...
        request(options, function(err, res, body) {
            if(!parseResponseBody(err, res, body)) {
                return;
            }
            baseUrl = JSON.parse(body).loginAccounts[0].baseUrl;
            next(null); // call next function
        });
    },

    /////////////////////////////////////////////////////////////////////////////////////
    // Step 2: Request Signature on a PDF Document
    /////////////////////////////////////////////////////////////////////////////////////
    function(next) {    
        var url = baseUrl + "/envelopes";
        // following request body will place 1 signature tab 100 pixels to the right and
        // 100 pixels down from the top left of the document that you send in the request
        var body = {
            "recipients": {
                "signers": [{
                    "email": email,
                    "name": recipientName,
                    "recipientId": 1,
                    "tabs": {
                        "signHereTabs": [{
                            "xPosition": "100",
                            "yPosition": "100",
                            "documentId": "1",
                            "pageNumber": "1"                       


                        }]
                    }
                }]
            },
            "emailSubject": 'DocuSign API - Signature Request on Document Call',
            "documents": [{
                "name": documentName,
                "documentId": 1,
            }],
            "status": "sent",
        };

        // set request url, method, body, and headers
        var options = initializeRequest(url, "POST", body, email, password);

        // change default Content-Type header from "application/json" to "multipart/form-data"
        options.headers["Content-Type"] = "multipart/form-data";

        // configure a multipart http request with JSON body and document bytes
        options.multipart = [{
                    "Content-Type": "application/json",
                    "Content-Disposition": "form-data",
                    "body": JSON.stringify(body),
                }, {
                    "Content-Type": "application/pdf",
                    'Content-Disposition': 'file; filename="' + documentName + '"; 

documentId=1',
                    "body": fs.readFileSync(documentName),
                }
        ];

        // send the request...
        request(options, function(err, res, body) {
            parseResponseBody(err, res, body);
        });
    } // end function   
]);

//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//***********************************************************************************************
function initializeRequest(url, method, body, email, password) {    
    var options = {
        "method": method,
        "uri": url,
        "body": body,
        "headers": {}
    };
    addRequestHeaders(options, email, password);
    return options;
}

///////////////////////////////////////////////////////////////////////////////////////////////
function addRequestHeaders(options, email, password) {  
    // JSON formatted authentication header (XML format allowed as well)
    dsAuthHeader = JSON.stringify({
        "Username": email,
        "Password": password, 
        "IntegratorKey": integratorKey  // global
    });
    // DocuSign authorization header
    options.headers["X-DocuSign-Authentication"] = dsAuthHeader;
}

///////////////////////////////////////////////////////////////////////////////////////////////
function parseResponseBody(err, res, body) {
    console.log("\r\nAPI Call Result: \r\n", JSON.parse(body));
    if( res.statusCode != 200 && res.statusCode != 201) { // success statuses
        console.log("Error calling webservice, status is: ", res.statusCode);
        console.log("\r\n", err);
        return false;
    }
    return true;
}

再次,我對此很陌生,因此,非常感謝您的幫助,在此先感謝您!

更新-在意識到我無法在QuickBase代碼頁上使用的node.js片段之后,我嘗試了以下兩個編碼選項,但都沒有起作用……任何想法我做錯了什么?

var xhr = createCORSRequest('GET', urlLoginCall);
xhr.withCredentials = true;
xhr.setRequestHeader('X-DocuSign-Authentication', jsonLoginCall);
xhr.send(); 
xhr.onload = function() {
    var responseText = xhr.responseText;
    alert(responseText);
};
xhr.onerror = function() {
    alert('There was an error!');
};

$.ajax({
    type: 'GET',
    url: urlLoginCall,
    headers: {
        'X-DocuSign-Authentication': jsonLoginCall
    },
    xhrFields: {
        withCredentials: true
    },
    success: function() {
        alert("success");
    },
    error: function() {
        alert("failure");
    }
});

通過Fidler,我總是將其作為請求標頭:

OPTIONS /restapi/v2/login_information HTTP/1.1
Host: demo.docusign.net
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: https://twmts.quickbase.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Access-Control-Request-Headers: x-docusign-authentication
Accept: */*
Referer: https://twmts.quickbase.com/db/bhkhmzax6?a=dbpage&pageID=145
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

這作為響應頭:

HTTP/1.1 200 OK
Cache-Control: no-cache
Date: Mon, 10 Mar 2014 12:15:48 GMT
Content-Length: 0
Strict-Transport-Security: max-age=7776000; includeSubDomains

沒有返回XML或JSON。 我知道這一定很簡單,我只是不明白,但是我花了很多時間tryig來確定我所缺少的東西,除了不使用node.js之外,我無法弄清楚。

您引用的DocuSign示例代碼使用Node.js模塊,更具體地說,它使用requestasyncfs模塊。 您似乎已經注釋了導入所需的Node.js庫的三行:

//var     async = require("async"),     // async module
//        request = require("request"),     // request module
//      fs = require("fs");         // fs module

require語句加載不同的模塊,並且當您注釋掉上面的代碼時,它不知道async.waterfall函數是什么,因為它沒有在任何地方聲明。 要解決該問題,請嘗試取消注釋上面的代碼,結果如下:

var     async = require("async"),     // async module
        request = require("request"),     // request module
      fs = require("fs");         // fs module

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM