docusign / docusign-esign-csharp-client

The Official Docusign C# Client Library used to interact with the eSignature REST API. Send, sign, and approve documents using this client.
https://developers.docusign.com/docs/esign-rest-api/sdks/csharp/
MIT License
129 stars 159 forks source link

Unexpected PEM Exception error (jwt-framework solution) #270

Closed cfleetwood closed 4 years ago

cfleetwood commented 4 years ago

Hello all,

I am working with the jwt-framework demo solution.

Working in the Docusign Admin / API and Keys area, I have created my User ID, App / Integration Key,, API Account ID and RSA Keypairs information. I have also set the Account's Base URI to https://demo.docusign.net.

Following the setup instructions, I modified the default app.config file with my configuration information.

Running the project in debug mode, I am encountering an "Unexpected PEM type" exception at the following line:

       {
            OAuth.OAuthToken authToken = ApiClient.RequestJWTUserToken(DSConfig.ClientID,
                            DSConfig.ImpersonatedUserGuid,
                            DSConfig.AuthServer,
                            Encoding.UTF8.GetBytes(DSConfig.PrivateKey),
                            1);

Although similar questions/issues raised received answers about putting the key information in .pem files, the demo project I am using has the key information hardcoded in the app.config file as follows:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="DS_CLIENT_ID" value="94eb2ea6-a516-4d99-9bca-66c0a941bc2b" />
    <add key="DS_IMPERSONATED_USER_GUID" value="0500d4b4-0fd5-4ba3-b73d-b21dcd5e144f" />
    <add key="DS_TARGET_ACCOUNT_ID" value="198ffcf2-421e-40e8-88dd-3410b2f4a3ef" />
    <add key="DS_SIGNER_1_EMAIL" value="cfleetwood@smartstart.org" />
    <add key="DS_SIGNER_1_NAME" value="Chris Fleetwood" />
    <add key="DS_CC_1_EMAIL" value="blkwebman@mindspring.com" />
    <add key="DS_CC_1_NAME" value="Christopher Fleetwood" />
    <add key="DS_AUTH_SERVER" value="https://account-d.docusign.com"/>
    <add key="DS_PRIVATE_KEY" value="MIIEpAIBAAKCAQEAnPxio7eUI18Qyok6BgkE3B4odA5W6uB+sDsiD8qNClhtarvV
pf3dOpgow8xht9A23Q7ZWcjdsjKDqVl6mlhW0m3isTuwbVIqsgRo6+C5gOxIxsO2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CUF8NKkC+vzowHx9XTjEWiM7+l1GoM
6IHtFyI0S6MM3/ijb3f+4uUCgYEAh+4IndJQlA8/dN0qAaUGfnZ5vN5bN0eJFvKK
gMOqr2rsXza5zniCY1KewlH8tglo8Qh5MKPPJfV1ZJsx09EyYworNfMhTMQA+EjM
qbpVsN3jVk/RSdsa0lUKAphzR1oL0LBAUM2O6TyFH63qscVw2aS/kZ9pN8kebwas
S+ds86MCgYAiGx7RQSe4BKOWarBBGp5eewML8sPsLpMPqpOmrEPb+/EsarQKdIep
r1RqM3+jFUteGSVwby+KTWFjdToRz0sVFhCZQODM2nmtM+u/OP/D21qd8FxGw1WK
UqmIPO5ZFdaZ/eeEMGqI9GwpRYK15JKCJ3cVNibJnIrX5xt0PzhOJQ==" />
  </appSettings>
</configuration>

DS_CLIENT_ID is the Integration Key DS_IMPERSONATED_USER_GUID is the User ID DS_AUTH_SERVER is the Account's Base URI DS_PRIVATE_KEY is the private key component of the RSA Keypair (minus the "Begin Private Key"/"End Private Key" strings)

Any ideas as to what is causing this exception? Any insights and/or suggestions would, of course, be greatly appreciated.

Regards,

Chris Fleetwood Software Developer North Carolina Partnership for Children, Inc. cfleetwood@smartstart.org

mmallis87 commented 4 years ago

@cfleetwood you need to add &#xA; (line feed) at the end of each line of DS_PRIVATE_KEY.

Here is an example: https://github.com/docusign/eg-01-csharp-jwt-core/blob/master/common/App.config_example.txt#L12-L21

cfleetwood commented 4 years ago

@mmallis87 Thanks for the info. That resolved that particular exception, but now I'm encountering the following exception/error instead:

Sending an envelope with three documents. This takes about 15 seconds...
Obtaining a new access token...

DocuSign Exception!
C O N S E N T   R E Q U I R E D
Ask the user who will be impersonated to run the following url:

    https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature impersonation&client_id=94eb2ea6-a516-4d99-9bca-66c0a941bc2b&redirect_uri=https://www.docusign.com

It will ask the user to login and to approve access by your application.
Alternatively, an Administrator can use Organization Administration to
pre-approve one or more users.
Done. Hit enter to exit...

Based on the documentation, I thought that the integration key was the primary authentication for the authorized application. I have a specific application defined under an organization definition and I'm referencing the integration key in my app.config flle. Am I missing something? I do have two (2) users/emails specified in the app.config file (I thought they were the "recipients").

Chris Fleetwood Software Developer North Carolina Partnership for Children, Inc. cfleetwood@smartstart.org

mmallis87 commented 4 years ago

@cfleetwood this is totally normal: the first time you use JWT it will ask you to grant access to the integration key you're using. This is because the key is "impersonating" you (your user ID). It's one-time deal. You won't need to do it again unless you revoke access to this I.K.

All you have to do is to open this url in the browser and login if needed: https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature%20impersonation&client_id=94eb2ea6-a516-4d99-9bca-66c0a941bc2b&redirect_uri=https://www.docusign.com

More on this topic: https://developers.docusign.com/esign-rest-api/guides/authentication/obtaining-consent

cfleetwood commented 4 years ago

@mmallis87 OK. I tried the link you posted and I do get to the Docusign login screen, but the following error message also displays in RED:

Invalid authentication request: The response type is not supported.

The above error reappears after i enter my login/password info.

I imagine this is a "first-time" error as well.

Chris Fleetwood Software Developer North Carolina Partnership for Children, Inc. cfleetwood@smartstart.org

mmallis87 commented 4 years ago

@cfleetwood do you mind sharing your I.K. settings? Here is an example of what I'm looking for:

cfleetwood commented 4 years ago

@mmallis87 Here are my integration key settings: image image image

One difference I noticed that, while you had Authentication set to Authorization Code Grant, I had it set to Implicit Grant. So I changed that accordingly.

I also noticed that your example had no Redirect URI set: my config had it set to http://www.smartstart.org, my employers' corporate website home page.

However, when I re-ran the code, got the "approve" user error and URL, and then pasted it in a browser, I got the login screen and the following error:

The redirect URI is not registered properly with DocuSign

So, since your example had no Redirect URI set, I removed the setting, I re-ran the code, got the "approve" user error and URL, and then pasted it in a browser, I got the login screen and the following error:

There are no redirect URIs registered with DocuSign

So I reentered the URI and saved the integration key definition.

The URI is saved in the API & Keys definition so I don't understand what I'm missing.

Chris Fleetwood Software Developer North Carolina Partnership for Children, Inc. cfleetwood@smartstart.org

mmallis87 commented 4 years ago

@cfleetwood the consent URL should include the redirect uri that you have put in the I.K. settings. In your case it's https://account-d.docusign.com/oauth/auth?response_type=code&scope=signature%20impersonation&client_id=94eb2ea6-a516-4d99-9bca-66c0a941bc2b&redirect_uri=http://www.smartstart.org/

cfleetwood commented 4 years ago

@mmallis87 That worked; I was able to get the consent for the cfleetwood@smartstart.org userid.

My test project now works. I ran the test project and received the following email:

image

Many thanks for your assistance!

Chris Fleetwood Software Developer North Carolina Partnership for Children, Inc. cfleetwood@smartstart.org

mmallis87 commented 4 years ago

@cfleetwood I'm glad your integration is working now. Happy DocuSigning!