Pradeep Yadav

Pradeep Yadav

  • 322
  • 5.6k
  • 2.7m

Pass token to API

Jan 27 2020 12:36 PM
Hi Team,
I am working on asp.Net MVC Web WEP, I have to call third party API, in which we need to pass token and get response,However when I am passing token and trying to execute the api getiting error for of Authorization i.e. 401,
Below is the code fopr the same ,
 
 using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://mata-design-api.idevdesign.net/");
                client.DefaultRequestHeaders.Accept.Clear();




                client.DefaultRequestHeaders.Add("Accept", "application/vnd.com.ttpapps.mata.admin+json");
                client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");
                client.DefaultRequestHeaders.Add("Authorization", "Bear xoE3BHvs3DEf4hJXgk7F2UFpphvJ1PEaljINuDHl2hPkn1SOs_tjKeS4aqfPGCFaD_xVRbugbcqQWlndmTZGsg0LBQx8jWXVSKUEqJQzfvaG8b3GYa5B31WafWMOB4A6bx573O10Pk86aF0dqvWuR48RRNpLM0XkL5AoL2Ti9ugwk_uJv2sfehw-4pVB8JpbLtKw02Rz_0U0yh-dnqaG5oWUGr8EblYGM4rdSijIul87oRDouYqjJj5OeFEy1a6z1Rnn8F543HE9sinYsf3t2EgSqzUW1NWscJADJgBvjn4V8h3hTpzxT4-EkDW8hbD7fSudJq2kpNWhZhMU_z89xPBmbMLmmx-wZDwlr_i-hPbe-lx9IcRRdUFCy1fZpIyHhyqaQ3B6Yn_INo34ElIo");

                var body = @"grant_type=password&username=tvmadmin&password=96n%7DB7%5B%24V*tVz%40Fx&client_id=tvm&client_secret=9guNHwvmRzUzrPT2bOndeZKmRLME3gQpbpM4C0AgnrHkPhw&device_id=c9e80ad5-9114-4184-9e08-7a209383d639";

                var json = JsonConvert.SerializeObject(body);
                var data = new StringContent(json, Encoding.UTF8, "application/json");
               
                // client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //GET Method 
                HttpResponseMessage response = await client.PostAsync("https://mata-design-api.idevdesign.net/store/ticket-types", data);
                if (response.IsSuccessStatusCode)
                {
                    string token = await response.Content.ReadAsStringAsync();
                    return token;
                }
                else
                {
                    Console.WriteLine("Internal server Error");
                    return "";
                }
            }

Answers (9)