简体   繁体   中英

Docusign /oauth/token endpoint return page instead of json with bearer C#

I went through the OAuth2 proccess in DocuSign API, I follow all the steps using official docs, but when I tried to perform the request in order to get the the AccessToken I received an HTML as response, indicating something like "DocuSign is temporarily unavailable. Please try again momentarily." Although the http response is 200(OK), The weird stuff is when I test with the same values on Postman I get the correct response.

This is my code

public static DocuSignBearerToken GetBearerToken(string AccessCode, bool RefreshToken = false)
    {
        string AuthHeader = string.Format("{0}:{1}", DocuSignConfig.IntegratorKey, DocuSignConfig.SecretKey);
        var client = new RestClient("http://account-d.docusign.com");
        client.Authenticator = new HttpBasicAuthenticator(DocuSignConfig.IntegratorKey, DocuSignConfig.SecretKey);
        var request = new RestRequest("/oauth/token", Method.POST);
        request.AddHeader("content-type", "application/x-www-form-urlencoded");
        request.AddHeader("authorization", "Basic " + Base64Encode(AuthHeader));
        if(!RefreshToken)
            request.AddParameter("application/x-www-form-urlencoded", string.Format("grant_type=authorization_code&code={0}", AccessCode), ParameterType.RequestBody);
        else
            request.AddParameter("application/x-www-form-urlencoded", string.Format("grant_type=refresh_token&refresh_token={0}", AccessCode), ParameterType.RequestBody);

        IRestResponse response = client.Execute(request);

        var responseString = response.Content;
        DocuSignBearerToken Result = JsonConvert.DeserializeObject<DocuSignBearerToken>(responseString);

        return Result;
    }

好的,这很尴尬,阅读他们从未指定授权URL是http或https的DocuSign文档(我以为它是http),邮递员足够聪明,可以在执行请求时确定http或https,而我的代码却没有,只需更改从http://到https://的授权URL解决了该错误。

If your tests using Postman work, then there is a problem with your code.

We've all been there, including me!

In these cases, I send my request to requestb.in to see what I'm really sending to the server. You'll find something is different from what you're sending via Postman.

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