微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DocuSign授权码授予访问令牌端点每次都返回错误请求

如何解决DocuSign授权码授予访问令牌端点每次都返回错误请求

我尝试了不同的方式来传递headercontent,但是每次从bad request获取docusign获取访问令牌。

下面是回调Web API方法,该方法在请求authorization code之后被调用

[HttpGet]
 [AllowAnonymous]
 [Route("CallBack")]
 public string Callback()
 {
     string accesstoken = "";
     try
     {
         var response = Request.Query;
         if (Request.Query != null && Request.Query.Keys != null && Request.Query.Keys.Count > 0)
         {
             var authorizationCode = Request.Query["code"];

             /*                    
                     Request => POST https://account-d.docusign.com/oauth/token
                     Content-Type: application/x-www-form-urlencoded
                     Header => Authorization: Basic BASE64_COMBINATION_OF_INTEGRATOR_AND_SECRET_KEYS
                     Data => grant_type=authorization_code&authorization_code=YOUR_AUTHORIZATION_CODE
             */

             //Body
             var keyvalues = new Dictionary<string,string>();
             keyvalues.Add("grant_type","authorization_code");
             keyvalues.Add("authorization_code",authorizationCode);
             var bodyContent = new FormUrlEncodedContent(keyvalues);

             //Header
             //Content-Type: application/x-www-form-urlencoded
             //Authorization: Basic BASE64_COMBINATION_OF_INTEGRATOR_AND_SECRET_KEYS 
             string base64Decoded = configuration.IntegrationKey + ":" + configuration.SecretKey;
             string base64Encoded;
             byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Decoded);
             base64Encoded = System.Convert.ToBase64String(data);

             var client = new System.Net.Http.HttpClient();
             client.BaseAddress = new Uri("https://account-d.docusign.com");
             var request = new System.Net.Http.HttpRequestMessage(HttpMethod.Post,"https://account-d.docusign.com/oauth/token");
             request.Content = bodyContent;

             request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
             request.Headers.Authorization = new AuthenticationHeaderValue("Basic",base64Encoded);

             var accesstokenresponse = client.SendAsync(request).Result;

             //Set access token
             //accesstoken
         }
     }
     catch (Exception ex)
     {

     }

     return accesstoken;
 }

解决方法

一些适合您的想法:

  1. 最好使用OAuth2客户端库。这是InfoSec专家和DocuSign的高度建议。
  2. 如果您决定采用自己的实现方式:请通过Postman或类似工具尝试OAuth2流程,以确保您确切了解该流程的工作原理。
  3. 请记住,授权码是有时间限制的。您需要在一分钟左右的时间内将其交换为访问令牌(可能更少,我没有确切的数据)。
  4. 使用wireshark或类似工具来准确查看要发送到DocuSign的内容。
  5. 重要的InfoSec问题:在state参数中发送一个现时值,然后在从DocuSign收到第一个响应时检查它是否相同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。