Java Docusign身份验证

时间:2019-02-13 09:06:23

标签: java docusignapi

我正在尝试使用Java连接docusign。 下面的代码我正在使用。

public class DocuSignExample1 {

    private static final String Recipient = "xxx@gmail.com";
    private static final String SignTest1File = "/src/test/docs/SignTest1.pdf";
    private static final String BaseUrl = "https://demo.docusign.net/restapi";
    private static final String IntegratorKey = "xxxxx";
    private static final String UserId = "xxxxxx";
    private static final String privateKeyFullPath = System.getProperty("user.dir") + "/src/test/keys/docusign_private_key2.txt";
    public static void main(String[] args) {

        System.out.println("\nRequestASignatureTest:\n" + "===========================================");
        byte[] fileBytes = null;
        try {
            String currentDir = System.getProperty("user.dir");

            Path path = Paths.get(currentDir + SignTest1File);
            fileBytes = Files.readAllBytes(path);
        } catch (IOException ioExcp) {
            ioExcp.printStackTrace();
        }

        EnvelopeDefinition envDef = new EnvelopeDefinition();
        envDef.setEmailSubject("Please Sign My Sample Document");
        envDef.setEmailBlurb("Hello, Please Sign My Sample Document.");

        // add a document to the envelope
        Document doc = new Document();
        String base64Doc = Base64.encodeToString(fileBytes, false);
        doc.setDocumentBase64(base64Doc);
        doc.setName("TestFile.pdf");
        doc.setDocumentId("1");

        List<Document> docs = new ArrayList<Document>();
        docs.add(doc);
        envDef.setDocuments(docs);

        // Add a recipient to sign the document
        Signer signer = new Signer();
        signer.setEmail(Recipient);
        signer.setName("Sanjay");
        signer.setRecipientId("1");

        // Above causes issue
        envDef.setRecipients(new Recipients());
        envDef.getRecipients().setSigners(new ArrayList<Signer>());
        envDef.getRecipients().getSigners().add(signer);

        // send the envelope (otherwise it will be "created" in the Draft folder
        envDef.setStatus("sent");

        ApiClient apiClient = new ApiClient(BaseUrl);
        try {
            byte[] privateKeyBytes = null;
            try {
                privateKeyBytes = Files.readAllBytes(Paths.get(privateKeyFullPath));
            } catch (IOException ioExcp) {
                Assert.assertEquals(null, ioExcp);
            }
            if (privateKeyBytes == null)
                return;

            java.util.List<String> scopes = new ArrayList<String>();
            scopes.add(OAuth.Scope_SIGNATURE);

            OAuth.OAuthToken oAuthToken = apiClient.requestJWTUserToken(IntegratorKey, UserId, scopes, privateKeyBytes,
                    3600);
            Assert.assertNotSame(null, oAuthToken);
            // now that the API client has an OAuth token, let's use it in all
            // DocuSign APIs
            apiClient.setAccessToken(oAuthToken.getAccessToken(), oAuthToken.getExpiresIn());
            UserInfo userInfo = apiClient.getUserInfo(oAuthToken.getAccessToken());

            System.out.println("UserInfo: " + userInfo);

            apiClient.setBasePath(userInfo.getAccounts().get(0).getBaseUri() + "/restapi");
            Configuration.setDefaultApiClient(apiClient);
            String accountId = userInfo.getAccounts().get(0).getAccountId();

            EnvelopesApi envelopesApi = new EnvelopesApi();

            EnvelopeSummary envelopeSummary = envelopesApi.createEnvelope(accountId, envDef);
            System.out.println("EnvelopeSummary: " + envelopeSummary);
        } catch (ApiException ex) {
            ex.printStackTrace();
            System.out.println("Exception: " + ex);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception: " + e.getLocalizedMessage());
        }
    }
}

enter image description here

我正在从给定的图像中复制clientid和集成密钥。

错误:请求访问令牌时出错:POST https://account-d.docusign.com/oauth/token返回了400错误请求的响应状态

3 个答案:

答案 0 :(得分:1)

通常,public function execute(Varien_Event_Observer $observer) { $block = $observer->getData('block'); if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs) { $block->removeTab('related'); $block->setTabData('upsell', 'label', Mage::helper('catalog')->__('[New Name Here]')); } } 响应表明您正在发送的请求正文有问题或其他有关请求的错误格式。要解决此问题,我建议您在发出请求之前先打印您的请求正文(即信封定义),以便检查其内容并确保它符合您的期望。

至少要发送一个信封,您需要一个电子邮件主题,文档,收件人和状态(设置为400 Bad Request)。

当您使用JSON打印请求正文时,它应如下所示:

"sent"

答案 1 :(得分:1)

从您的评论到Ergin的回答,听起来好像您没有完成JWT身份验证流程。

我建议您从针对您的语言的DocuSign JWT Grant代码示例开始:

有关安装和配置说明,请参见自述文件。

一旦您能够通过JWT流生成访问令牌,就可以继续实现自己的DocuSign API调用。

答案 2 :(得分:-1)

我接受同意400错误后也遇到了同样的问题。 您可以通过在浏览器中输入此URL来接受同意

https://account-d.docusign.com/oauth/auth?response_type=token&scope=signature&client_id=<integrator key>&state=<random number to avoid request forgery>&redirect_uri=http://example.com/callback/