Reputation: 1
Afternoon! I am very new to JavaScript and the DocuSign API. I tried following the sample, but I am getting a ReferenceError.
For testing purposes, I have async.js, fs.js, request.js, and require.js at the root of the folder on my PC along with the test HTML doc
HTML:
<html>
<head>
<script src="require.js"></script>
<script src="example.js"></script>
</head>
<body>
<div id="message"></div>
</body>
</html>
I am getting the following error: ReferenceError: async is not defined async.waterfall(
Below is the example.js code I got from the DocuSign website, slighlt adjusted based on another error around the require function:
// 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;
}
Again, I am very new to this, so any help would be very appreciated, thanks in advance!
UPDATE - after realizing I could not takw pieces of node.js to use on QuickBase code pages, I attempted the below two coding options, neither worked...any ideas what I am doing wrong?
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!');
};
and
$.ajax({
type: 'GET',
url: urlLoginCall,
headers: {
'X-DocuSign-Authentication': jsonLoginCall
},
xhrFields: {
withCredentials: true
},
success: function() {
alert("success");
},
error: function() {
alert("failure");
}
});
Via Fidler, I always get this as the request header:
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
an this as the response header:
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
No XML or JSON returned. I know it has to besomething simple I am just not getting, but I have spent a lot of time tryig to determine what I am missing and other than not using the node.js, I can't figure it out.
Upvotes: 0
Views: 1843
Reputation: 9356
The DocuSign sample code you've referenced uses Node.js
modules, more specifically it uses request
, async
, and the fs
module. It looks like you've commented out the three lines where it imports the Node.js libraries that it needs:
//var async = require("async"), // async module
// request = require("request"), // request module
// fs = require("fs"); // fs module
The require
statement loads different modules, and when you comment out the above the code it has no idea what the async.waterfall function is since it's not declared anywhere. To resolve try un-commenting the above code so that's it's:
var async = require("async"), // async module
request = require("request"), // request module
fs = require("fs"); // fs module
Upvotes: 1