C#中调用DocuSign FoldersApi遇认证失败错误,请求排查
问题描述
我从DocuSign官网下载了基于JWT授权的远程签署控制台应用,应用本身能正常运行,但调用FoldersApi时抛出错误:USER_AUTHENTICATION_FAILED One or both of Username and Password are invalid. Invalid access token。我想知道这个问题是否和apiClient有关,以及为什么之前发送邮件正常,现在却出现认证错误。
相关代码如下:
namespace DocuSign.CodeExamples.JWT_Console { class Program { static readonly string DevCenterPage = "https://developers.docusign.com/platform/auth/consent"; static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.White; OAuthToken accessToken = null; try { accessToken = JWTAuth.AuthenticateWithJWT("ESignature", ConfigurationManager.AppSettings["ClientId"], ConfigurationManager.AppSettings["ImpersonatedUserId"], ConfigurationManager.AppSettings["AuthServer"], ConfigurationManager.AppSettings["PrivateKeyFile"]); } catch (ApiException apiExp) { //Removed this section because it is not helpful to the question } var apiClient = new ApiClient(); apiClient.SetOAuthBasePath(ConfigurationManager.AppSettings["AuthServer"]); UserInfo userInfo = apiClient.GetUserInfo(accessToken.access_token); Account acct = userInfo.Accounts.FirstOrDefault(); FoldersApi foldersApi = new FoldersApi(apiClient); foreach (var folder in foldersApi.List(acct.AccountId).Folders) { Console.WriteLine(folder.Name); } } } }
调试截图:
问题分析与解决
核心原因
你的apiClient没有绑定获取到的JWT访问令牌,导致调用FoldersApi时缺少有效认证信息。之前发送邮件正常,大概率是当时的代码正确配置了令牌,而当前代码遗漏了这一步。另外,GetUserInfo能成功是因为该方法直接传入了access_token参数,不依赖apiClient自身的配置;但FoldersApi调用会读取apiClient的默认头信息,此时无令牌就会触发认证失败。
修复代码
在创建apiClient并设置OAuth基础路径后,添加令牌绑定代码:
var apiClient = new ApiClient(); apiClient.SetOAuthBasePath(ConfigurationManager.AppSettings["AuthServer"]); // 新增:将JWT令牌添加到apiClient的默认请求头 apiClient.Configuration.DefaultHeader.Add("Authorization", $"Bearer {accessToken.access_token}");
额外检查点
- 确认
accessToken获取成功:调试时查看accessToken.access_token是否有有效值,以及expires_in字段确认令牌未过期。 - 验证
ImpersonatedUserId和ClientId配置正确:确保配置文件中的用户ID和客户端ID与DocuSign后台一致。
内容的提问来源于stack exchange,提问作者hkn-a
相关产品推荐
相关产品推荐

