0

I'm just trying to play with the Docusign API for NodeJS in order to get document signing integration for an app I'm building. I think I have the basic auth handshake working. I created test routes at /docusign and /docusign/auth and I'm able to get an access token with the following code.

I just want to get a test signing link in my email from a doc I already uploaded as a tempalte, but the createEnvelope call ends in the up in catch with the error:

USER_AUTHENTICATION_FAILED One or both of Username and Password are invalid. Invalid access token

provide.generateAccessToken = (req, res, next) => {
apiClient.generateAccessToken(integratorKey, clientSecret, req.query.code, function (err, oAuthToken) {

    console.log(oAuthToken);
    apiClient.setBasePath(basePath);
    //IMPORTANT: In order to access the other api families, you will need to add this auth header to your apiClient.
    apiClient.addDefaultHeader('Authorization', 'Bearer ' + oAuthToken.accessToken);
    var envelopesApi = new docusign.EnvelopesApi();
    envelopesApi.apiClient.addDefaultHeader('Authorization', 'Bearer ' + oAuthToken.accessToken);
    apiClient.getUserInfo(oAuthToken.accessToken, function (err, userInfo) {
        console.log("UserInfo: " + userInfo);
        // parse first account's baseUrl
        // below code required for production, no effect in demo (same
        // domain)
        //apiClient.setBasePath(userInfo.accounts[0].baseUri + "/restapi");
       // create a new envelope object that we will manage the signature request through
        var envDef = new docusign.EnvelopeDefinition();
        envDef.emailSubject = 'Rental Application';
        envDef.templateId = '66c8c9cf-xxx-xxxx-xxxx-xxxxxxxx';

        // create a template role with a valid templateId and roleName and assign signer info
        var tRole = new docusign.TemplateRole();
        tRole.roleName = 'Applicant';
        tRole.name = 'test';
        tRole.email = '[email protected]';

        // create a list of template roles and add our newly created role
        var templateRolesList = [];
        templateRolesList.push(tRole);

        // assign template role(s) to the envelope
        envDef.templateRoles = templateRolesList;

        // send the envelope by setting |status| to 'sent'. To save as a draft set to 'created'
        envDef.status = 'sent';

        // use the |accountId| we retrieved through the Login API to create the Envelope
        var accountId = userInfo.sub;

        // instantiate a new EnvelopesApi object

        // call the createEnvelope() API
        envelopesApi.createEnvelope(accountId, {'envelopeDefinition': envDef}, function (err, envelopeSummary, response) {
        if (err) {
            return next(err);
        }
        console.log('EnvelopeSummary: ' + JSON.stringify(envelopeSummary));
        return JSON.stringify(envelopeSummary);
        });
    });
});

}

Any help to point me in the right direction would be greatly appreciated!

Thanks!

1 Answer 1

1

Instead of

    var envelopesApi = new docusign.EnvelopesApi();

try

    var envelopesApi = new docusign.EnvelopesApi(apiClient);

Also, for a working example of Node with DocuSign and Authorization Code Grant, check out this example.

Sign up to request clarification or add additional context in comments.

1 Comment

This ended up being it. I actually wound up setting the baseUri and header manually: envelopesApi.apiClient.setBasePath(userInfo.accounts[0].baseUri + "/restapi"); envelopesApi.apiClient.addDefaultHeader('Authorization', 'Bearer ' + oAuthToken.accessToken); before realizing there is an setApiClient function. Your answer is cleaner, so I will accept it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.