简体   繁体   中英

DocuSign JWT Invalid Access Token

I have upgraded the DocuSign eSign NuGet package from 4.1.1 to the latest version 5.1.0 in my test project.

Test Code:

            var auth = DocuSign.eSignature.JWTAuth.AuthenticateWithJWT();
            string accessToken = auth.accessToken;
            string accountId = auth.accountId;
            string basePath = auth.baseUri;

            var config = new Configuration(basePath);
            config.AddDefaultHeader("Authorization", "Bearer " + accessToken);

            EnvelopeDefinition envelope = MakeEnvelope("user@somecompany.com", "User Name", "",
                "", "15ffda8-5953-4e5f-b9d6-112133adf0e7");

            var envelopesApi = new EnvelopesApi(new ApiClient(config));
            EnvelopeSummary result = envelopesApi.CreateEnvelope(accountId, envelope);

However, since then I am receiving the following error when running the code:

DocuSign.eSign.Client.ApiException: 'Error calling CreateEnvelope: {"errorCode":"USER_AUTHENTICATION_FAILED","message":"One or both of Username and Password are invalid. Invalid access token"}'

I got the AuthenticateWithJWT() method from DocuSign:

        public static (string accessToken, string accountId, string baseUri) AuthenticateWithJWT()
        {
            var apiClient = new ApiClient();
            string ik = ConfigurationManager.AppSettings["IntegrationKey"];
            string userId = ConfigurationManager.AppSettings["userId"];
            string authServer = ConfigurationManager.AppSettings["AuthServer"];
            string rsaKey = ConfigurationManager.AppSettings["RSAKey"];
            OAuth.OAuthToken authToken = apiClient.RequestJWTUserToken(ik,
                            userId,
                            authServer,
                            Encoding.UTF8.GetBytes(rsaKey),
                            1);

            string accessToken = authToken.access_token;
            apiClient.SetOAuthBasePath(authServer);
            OAuth.UserInfo userInfo = apiClient.GetUserInfo(authToken.access_token);
            Account acct = null;

            var accounts = userInfo.Accounts;
            {
                acct = accounts.FirstOrDefault(a => a.IsDefault == "true");
            }
            string accountId = acct.AccountId;
            string baseUri = acct.BaseUri + "/restapi";
            return (accessToken, accountId, baseUri);
        }

It is correctly returning my accountId, an access token, and a base path just like before.
Any ideas on what might be causing this error or what steps I might take to troubleshoot this further?

I was able to put together a solution for this:

            var auth = DocuSign.eSignature.JWTAuth.AuthenticateWithJWT();
            string accessToken = auth.accessToken;
            string accountId = auth.accountId;
            string basePath = auth.baseUri;

            var apiClient = new ApiClient(basePath);
            apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);

            EnvelopeDefinition envelope = MakeEnvelope("user@somecompany.com", "User Name", "",
                "", "15ffda8-5953-4e5f-b9d6-112133adf0e7");

            var envelopesApi = new EnvelopesApi(apiClient);
            EnvelopeSummary result = envelopesApi.CreateEnvelope(accountId, envelope);

You no longer have to create a separate Configuration object. You can pass the basePath directly into the ApiClient constructor. The new apiClient instance has a Configuration property on it where you can set the DefaultHeader with your accessToken information. Finally you can pass the configured apiClient instance into the EnvelopesApi constructor.

might mean you're hitting the wrong environment (ie a Prod Key against a Demo endpoint or a Demo Key against a Prod endpoint). so make sure the baseUri is correct for the access token/key you're using

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM