1

I have rest api url like this https://example.com/ProductAPI/api/V1/getproduct. This api has api_key,username and password.How can I access this api and getproduct details using c#.I tried like this

WebRequest request = WebRequest.Create(@"https://example.com/ProductAPI/api/V1/getproduct");

request.Method = "GET";
request.Headers.Add("api_key","5g13441f-6915-4a34-8a53-bcb4er88b554");
request.Headers["Authorization"] = "Basic" + Convert.ToBase64String(Encoding.Default.GetBytes("admin:123"));
request.ContentLength = 0;
request.ContentType = @"application/json; charset=utf-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
Console.WriteLine(jsonResponse);
Console.ReadLine();
}

But it show 401 unauthorized error.Pls help to solve this

  • Check the API document to see whether the API needs api_key, user name and password in the Headers or in the body and format your request accordingly. – VinothNair Jul 18 '16 at 06:27
  • it works in swagger when I add api_key and credentials –  Jul 18 '16 at 06:34
  • use fiddler and see what is the difference in request format compare to the swagger request. – VinothNair Jul 18 '16 at 06:39
  • In browser console token can be generated from another url like https://example.com/ProductAPI/token?client_id="" –  Jul 18 '16 at 10:28

0 Answers0