Getting "Integrator key not found" while trying to download the document from DocuSign

43 views Asked by At

I can able to create envelope and send the document to the recipient to get the eSignature. After the document is completed when i try to download the document

Envelope envelope = envelopesApi.getEnvelope(accountId,envelopeId); byte[] results = envelopesApi.getDocument(accountId,envelopeId,"33");

Getting Error!!! com.docusign.esign.client.ApiException: Error while requesting server, received a non successful HTTP code 400 with response Body: '{"errorCode":"PARTNER_AUTHENTICATION_FAILED","message":"The specified Integrator Key was not found or is disabled. Invalid account specified for user."}'

Create Envelope (Working fine with JWT)

package com.sripy.demo;


import com.docusign.esign.api.AccountsApi;
import com.docusign.esign.api.EnvelopesApi;
import com.docusign.esign.client.ApiClient;
import com.docusign.esign.client.auth.OAuth.OAuthToken;
import com.docusign.esign.client.auth.OAuth.UserInfo;
import com.docusign.esign.client.ApiException;
import com.docusign.esign.model.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.io.*;

/**
 * Starter class for JWTConsoleApp application.
 */

public class JWTConsoleAppCreateEnvelope {

    static String DevCenterPage = "https://developers.docusign.com/platform/auth/consent";

    /**
     * Application entry point.
     *
     * @param args application command line arguments
     */
    public static void main(String[] args) throws IOException {
        String signerEmail = "******@yahoo.com";
        String signerName = "sriram sundararajan";
        String ccEmail = "******@gmail.com";
        String ccName = "John Doe";

        String className = "javax.ws.rs.client.ClientBuilder";

        try {
            Class<?> clazz = Class.forName(className);
            String classPath = clazz.getProtectionDomain().getCodeSource().getLocation().toString();
            System.out.println("Loaded " + className + " from: " + classPath);
        } catch (ClassNotFoundException e) {
            System.out.println("Class " + className + " not found!");
        }

        try {
// Get access token and accountId
            ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
            apiClient.setOAuthBasePath("account-d.docusign.com");
            ArrayList<String> scopes = new ArrayList<String>();
            scopes.add("signature");
            scopes.add("impersonation");
            scopes.add("envelope_read");
            scopes.add("envelope_send");
            scopes.add("envelope_write");


            byte[] privateKeyBytes = Files.readAllBytes(Paths.get("/...../src/main/resources/privatekey"));
            OAuthToken oAuthToken = apiClient.requestJWTUserToken("*****", "*****", scopes, privateKeyBytes, 3600);
            String accessToken = oAuthToken.getAccessToken();
            UserInfo userInfo = apiClient.getUserInfo(accessToken);
            apiClient.setAccessToken(accessToken, Long.valueOf("3600"));

            String accountId = userInfo.getAccounts().get(0).getAccountId();

// Check account association
            AccountsApi accountsApi = new AccountsApi(apiClient);
            AccountInformation accountInfo = accountsApi.getAccountInformation(accountId);


// Create envelopeDefinition object
            EnvelopeDefinition envelope = new EnvelopeDefinition();
            envelope.setEmailSubject("Please sign this document set");
            envelope.setStatus("sent");

// Create tabs object
            SignHere signHere = new SignHere();
            signHere.setDocumentId("1");
            signHere.setPageNumber("1");
            signHere.setXPosition("191");
            signHere.setYPosition("148");
            Tabs tabs = new Tabs();
            tabs.setSignHereTabs(Arrays.asList(signHere));
// Set recipients
            Signer signer = new Signer();
            signer.setEmail(signerEmail);
            signer.setName(signerName);
            signer.recipientId("1");
            signer.setTabs(tabs);
            CarbonCopy cc = new CarbonCopy();
            cc.setEmail(ccEmail);
            cc.setName(ccName);
            cc.recipientId("2");
            Recipients recipients = new Recipients();
            recipients.setSigners(Arrays.asList(signer));
            recipients.setCarbonCopies(Arrays.asList(cc));
            envelope.setRecipients(recipients);

// Add document
            Document document = new Document();
            document.setDocumentBase64("VGhhbmtzIGZvciByZXZpZXdpbmcgdGhpcyEKCldlJ2xsIG1vdmUgZm9yd2FyZCBhcyBzb29uIGFzIHdlIGhlYXIgYmFjay4=");
            document.setName("doc1.txt");
            document.setFileExtension("txt");
            document.setDocumentId("1");
            envelope.setDocuments(Arrays.asList(document));

// Send envelope
            apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
            EnvelopesApi envelopesApi = new EnvelopesApi(apiClient);
            EnvelopeSummary results = envelopesApi.createEnvelope(accountId, envelope);
            System.out.println("Successfully sent envelope with envelopeId " + results.getEnvelopeId());

        } catch (ApiException exp) {
            if (exp.getMessage().contains("consent_required")) {
                try {
                    System.out.println("Consent required, please provide consent in browser window and then run this app again.");
//Desktop.getDesktop().browse(new URI("https://account-d.docusign.com/oauth/auth?response_type=code&scope=impersonation%20signature&client_id=" + prop.getProperty("clientId") + "&redirect_uri=" + DevCenterPage));
                } catch (Exception e) {
                    System.out.print("Error!!! ");
                    System.out.print(e.getMessage());
                }
            }
        } catch (Exception e) {
            System.out.print("Error!!! ");
            System.out.print(e.getMessage());
        }
    }

}

Download Document (Same like above code except its calling download document)

package com.sripy.demo;


import com.docusign.esign.api.AccountsApi;
import com.docusign.esign.api.EnvelopesApi;
import com.docusign.esign.client.ApiClient;
import com.docusign.esign.client.ApiException;
import com.docusign.esign.client.auth.OAuth.OAuthToken;
import com.docusign.esign.client.auth.OAuth.UserInfo;
import com.docusign.esign.model.*;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Starter class for JWTConsoleApp application.
 */

public class JWTConsoleAppDownloadDocument {

    static String DevCenterPage = "https://developers.docusign.com/platform/auth/consent";

    /**
     * Application entry point.
     *
     * @param args application command line arguments
     */
    public static void main(String[] args) throws IOException {
        String signerEmail = "******@yahoo.com";
        String signerName = "sriram sundararajan";
        String ccEmail = "******@gmail.com";
        String ccName = "John Doe";


        try {
// Get access token and accountId
            ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
            apiClient.setOAuthBasePath("account-d.docusign.com");
            ArrayList<String> scopes = new ArrayList<String>();
            scopes.add("signature");
            scopes.add("impersonation");
            scopes.add("envelope_read");
            scopes.add("envelope_send");
            scopes.add("envelope_write");


            byte[] privateKeyBytes = Files.readAllBytes(Paths.get("/...../src/main/resources/private.key"));
            OAuthToken oAuthToken = apiClient.requestJWTUserToken("******", "*******", scopes, privateKeyBytes, 3600);
            String accessToken = oAuthToken.getAccessToken();
            UserInfo userInfo = apiClient.getUserInfo(accessToken);
            apiClient.setAccessToken(accessToken, Long.valueOf("3600"));

            String accountId = userInfo.getAccounts().get(0).getAccountId();
            String envelopeId = "3e07c8c9-7db7-47c4-a83f-f03bf229f7f7";

            downloadDoc(apiClient, envelopeId, accountId);

        } catch (ApiException exp) {
            if (exp.getMessage().contains("consent_required")) {
                try {
                    System.out.println("Consent required, please provide consent in browser window and then run this app again.");
//Desktop.getDesktop().browse(new URI("https://account-d.docusign.com/oauth/auth?response_type=code&scope=impersonation%20signature&client_id=" + prop.getProperty("clientId") + "&redirect_uri=" + DevCenterPage));
                } catch (Exception e) {
                    System.out.print("Error!!! ");
                    System.out.print(e.getMessage());
                }
            }
        } catch (Exception e) {
            System.out.print("Error!!! ");
            System.out.print(e.getMessage());
        }
    }

    private static void downloadDoc(ApiClient apiClient, String envelopeId, String accountId) {
        EnvelopesApi envelopesApi = new EnvelopesApi(apiClient);

        try {
            Envelope envelope = envelopesApi.getEnvelope(accountId, envelopeId);
            System.out.println(envelope.getAnySigner());
            byte[] results = envelopesApi.getDocument(accountId, envelopeId, "33");

            String dirPathString = "/...../src/main/resources/";
            String fileName = "DocuSign_POC_Signer_final.pdf";
            File f = new File(dirPathString + fileName);
            OutputStream os = new FileOutputStream(f);
// Starting writing the bytes in it
            os.write(results);

        } catch (ApiException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("Downloaded successfully");
    }

}
2

There are 2 answers

11
Karan Kaushik On

The error seems to indicate that you're specifying the wrong account Id for the request. Can you confirm that the account Id you're passing is the same one you've used to create the envelope?

0
Inbar Gazit On

This code is wrong, only need the first two (signature, impersonation) the last 3 you have in there are invalid.

       scopes.add("signature");
        scopes.add("impersonation");
        scopes.add("envelope_read");
        scopes.add("envelope_send");
        scopes.add("envelope_write");