简体   繁体   English

DocuSign JWT 无效的访问令牌

[英]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.我已在我的测试项目中将 DocuSign eSign NuGet 包从 4.1.1 升级到最新版本 5.1.0。

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"}' DocuSign.eSign.Client.ApiException: '调用 CreateEnvelope 时出错:{"errorCode":"USER_AUTHENTICATION_FAILED","message":"用户名和密码之一或两者无效。访问令牌无效"}'

I got the AuthenticateWithJWT() method from DocuSign:我从 DocuSign 得到了 AuthenticateWithJWT() 方法:

        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.它像以前一样正确返回我的 accountId、访问令牌和基本路径。
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.您不再需要创建单独的 Configuration 对象。 You can pass the basePath directly into the ApiClient constructor.您可以将 basePath 直接传递给 ApiClient 构造函数。 The new apiClient instance has a Configuration property on it where you can set the DefaultHeader with your accessToken information.新的 apiClient 实例有一个 Configuration 属性,您可以在其中使用您的 accessToken 信息设置 DefaultHeader。 Finally you can pass the configured apiClient instance into the EnvelopesApi constructor.最后,您可以将配置的 apiClient 实例传递给 EnvelopesApi 构造函数。

might mean you're hitting the wrong environment (ie a Prod Key against a Demo endpoint or a Demo Key against a Prod endpoint).可能意味着您遇到了错误的环境(即针对 Demo 端点的 Prod Key 或针对 Prod 端点的 Demo Key)。 so make sure the baseUri is correct for the access token/key you're using所以请确保 baseUri 对于您正在使用的访问令牌/密钥是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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