首页 > 解决方案 > 尝试使用docusign API获取authToken时出现错误请求

问题描述

我是 docuSign API 的新手,我按照 DocuSign 指南站点中的 C# 代码示例进行操作。我在尝试获取 AuthToken 时在这一步失败了。我的部分代码如下,与DocuSign示例基本相同,

public void OAuthAuthorizationCodeFlowTest()
{
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        // Make an API call with the token
        ApiClient apiClient = new ApiClient(RestApiUrl);
        DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;

        // Initiate the browser session to the Authentication server
        // so the user can login.
        string accountServerAuthUrl = 
        apiClient.GetAuthorizationUri(client_id, redirect_uri, true, 
        stateOptional);
        System.Diagnostics.Process.Start(accountServerAuthUrl);

        string accessToken = apiClient.GetOAuthToken(client_id, 
        client_secret, true, AccessCode);
        // login call is available in the authentication api 
        AuthenticationApi authApi = new AuthenticationApi();
        LoginInformation loginInfo = authApi.Login();

       // parse the first account ID that is returned (user might belong to 
       multiple accounts)
        AccountId = loginInfo.LoginAccounts[0].AccountId;
        BaseUri = loginInfo.LoginAccounts[0].BaseUrl;
        Console.WriteLine("accountId: " + AccountId);
        Console.WriteLine("base_uri: " + BaseUri);

client_id 是我的集成密钥,client_secret 是与该集成密钥相关的密钥,对吧?我检查了很多次,没有问题。我现在很困惑,为什么我仍然收到 400 错误。请在这里给我一些启发,谢谢!!!

标签: docusignapi

解决方案


System.Diagnostics.Process.Start(accountServerAuthUrl);将在浏览器上打开它,一旦您成功完成身份验证,浏览器将使用code=....回调 url ( redirect_uri)中的查询参数重定向

浏览器中的代码需要被你的WEBApp读取,然后你需要调用下面的代码来生成AccessToken:

string accessToken = apiClient.GetOAuthToken(client_id, 
        client_secret, true, AccessCode);

下面的代码是打开浏览器的一部分,理想情况下它是为了在独立系统上进行测试,在 WEBApp 上,您将浏览器重定向到accountServerAuthUrl

public void OAuthAuthorizationCodeFlowTest()
        {

            // Make an API call with the token
            ApiClient apiClient = new ApiClient(RestApiUrl);
            DocuSign.eSign.Client.Configuration.Default.ApiClient = apiClient;

            // Initiate the browser session to the Authentication server
            // so the user can login.
            string accountServerAuthUrl = apiClient.GetAuthorizationUri(client_id, redirect_uri, true, stateOptional);
            System.Diagnostics.Process.Start(accountServerAuthUrl);
        }

下面的代码在用户通过浏览器上的 DocuSign 身份验证并且浏览器被重定向到redirect_uriwith 时运行code=...,您的 Webapp 将读取此代码并将其发送AccessCode并从您的控制器调用下面的代码,

string accessToken = apiClient.GetOAuthToken(client_id, client_secret, true, AccessCode);

推荐阅读