0

I keep getting 400 bad request, but when using postman I am able to generate the token. When checking in fiddler i see the credential passing in {"key":"value", "key":value"}
but when sending the request in postman the credentials are passing like so key=value,key=value,

Also I think I have to add Authentication type Basic how do I do that?

[![enter image description here][1]][1]

[![enter image description here][2]][2]

Here is my Test method
[Test]
        [Obsolete]
        public void AuthenticationMechanism()
        {
            var client = new RestClient("http://examplegeneratetoken.com/");

            var request = new RestRequest("connect/token", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddHeader("cache-Control", "no-cache");
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddHeader("content-ecoding", "gzip");

            request.AddBody(new {grant_type="password",username="exampleabc@email.com",password="pass",scope="abc",client_id="postapi",client_secret="abc123"});
            var response = client.ExecutePostTaskAsync(request).GetAwaiter().GetResult();
            var access_token = response.DeserializeResponse()["access_token"];

        }

Here is my deserilization method 

public static class Libraries
    {
        public static Dictionary<string, string> DeserializeResponse(this IRestResponse restResponse)
        {
            var JSONObj = new JsonDeserializer().Deserialize<Dictionary<string, string>>(restResponse);

            return JSONObj;
        }

    }


[1]: https://i.stack.imgur.com/zJLEA.png
[2]: https://i.stack.imgur.com/HQn3o.png
Chetan
  • 6,711
  • 3
  • 22
  • 32
Johnny_347
  • 53
  • 1
  • 7
  • Have you checked what the request body looks like? For x-www-form-urlencoded it should be `grant_type=password&username=exampleabc@email.com&password=pass&scope=abc&client_id=postapi&client_secret=abc123` for your example. The OAuth2 specification states that while providing client credentials via Basic auth is recommended, they may also be provided in the body. For how to do Basic auth with RestClient see https://stackoverflow.com/a/50070946/7565574 - Basic auth username would be `postapi` and password `abc123`. Also please upload the images to StackOverflow, so they show up in your question. – ckuri Feb 19 '20 at 23:07
  • @ckuri yes i did, please look at the screen shot of the fiddler. Even when I pass it like that it gives me a 400 – Johnny_347 Feb 20 '20 at 00:35
  • In the error image you are sending in JSON format, but it should be in form-encoded format like in your success image. Then take a look at https://stackoverflow.com/a/46945038/7565574 which shows an example how to send form-encoded data with RestClient. – ckuri Feb 20 '20 at 06:59

0 Answers0