0

I was trying to get an access token to making API calls for docuSign.

For this I wrote a below code:

string CLIENT_ID = "5a63c30f-...-08453c10a995";
string SECRET_KEY = "ef7e08fc-....-321444f9e825";
byte[] ENCODED_KEYS = System.Convert.ToBase64String(Encoding.GetEncoding(28591).GetBytes($"{ CLIENT_ID }:{ SECRET_KEY }"));
string CONTENT_TYPE = "application/x-www-form-urlencoded";
string AUTHORIZATION = $"Basic {ENCODED_KEYS}";

var rest = new RestSharp.RestRequest();
rest.Method = RestSharp.Method.POST;
rest.AddHeader("Content-Type", CONTENT_TYPE);
rest.AddHeader("Authorization", AUTHORIZATION);

rest.AddParameter(new RestSharp.Parameter("code", CODE, RestSharp.ParameterType.RequestBody));
rest.AddParameter(new RestSharp.Parameter("grant_type", "authorization_code", RestSharp.ParameterType.RequestBody));
rest.AddParameter(new RestSharp.Parameter("redirect_uri", REDIRECT_URI, RestSharp.ParameterType.RequestBody));

rest.RequestFormat = RestSharp.DataFormat.Json;
string API_PATH = "https://account-d.docusign.com/oauth/token";
var client = new RestSharp.RestClient(API_PATH);
var result = client.Execute(rest);  

enter image description here

But when the same thing is run in Postman, it runs successfully.

enter image description here

It is really getting on my nerve, any clues?

EDIT: VERY IMPORTANT

I unchecked the Content-Type in the Postman

enter image description here

And then when I ran it, go the same error in Postman. What is happening

enter image description here

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • Is it supposed to be `string ENCODED_KEYS` instead of `byte[] ENCODED_KEYS` in your code? – Callum Watkins Jan 19 '21 at 04:13
  • No, it should be bytes only. – SimpleGuy Jan 19 '21 at 04:57
  • 1
    I don't quite understand how you're getting `ToBase64String` to return a `byte[]`, but regardless I would suggest comparing the Postman request with the RestSharp request to see what the difference is. [This answer](https://stackoverflow.com/a/39889201/4415734) to a similar question outlines some things that you can check. – Callum Watkins Jan 19 '21 at 05:01

1 Answers1

0

I believe the parameters you are adding are part of the request body, not URL parameters. Compare how the request is done in Postman where it's part of the body.

I highly recommend you use .NET libraries for this and not run this code your self. The C# SDK combined with .NET Core OAuth or .NET Framework can be used to do this without having to write the code yourself.

Inbar Gazit
  • 12,566
  • 1
  • 16
  • 23