0

So i´ve recently tried working with an api for the first time and i really don´t know what to do with the this api: https://skinbaron.de/misc/apidoc/ .I have already looked tutorials on how to call an api in c# but i still don´t really get what i have to do in my specific case. I´ve tried this: How do I make calls to a REST api using C#?

This question might seem stupid to people that know how to work with stuff like this but i have no expierence with api´s so far.

The exact code i´ve tried:

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey

    static void Main(string[] args)
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(URL);

        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

        HttpResponseMessage response = client.GetAsync(urlParameters).Result;  
        if (response.IsSuccessStatusCode)
        {

            // do something
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
        }
        client.Dispose();
    }
Real
  • 1
  • 1
  • 1
  • Use a sniffer like wireshark or fiddler and capture the http messages to see the format. Http has a header and a body. The parameters are html tags in the header and the post is the data in the body. – jdweng Feb 29 '20 at 16:30
  • Since the api is using Swagger (OpenAPI), you can use [NSwag Studio](https://github.com/RicoSuter/NSwag/wiki/NSwagStudio) to generate c# code. – Crowcoder Feb 29 '20 at 16:42
  • @Crowcoder should I be able to launch it right after executing the setup? Because i cant launch it. – Real Feb 29 '20 at 17:55

2 Answers2

1

You can use Rest sharp. Its really easy to use. This is an third party library used in c# to manage API's. No need to bother with any of the other details used to call API's.

var client = new RestClient("https://api.skinbaron.de/GetBalance");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Accept", "application/json");
request.AddParameter("apikey", "123");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
//Rest of the stuff
Hammad Shabbir
  • 722
  • 1
  • 6
  • 13
  • Sorry I didn't had a look of that. I misunderstood that. Now I have updated my answer – Hammad Shabbir Feb 29 '20 at 16:45
  • It returned "Error listing items: check your json input". – Real Feb 29 '20 at 17:53
  • I have updated the headers kindly have a look and try it. I have tried it out on swagger and its giving me unauthorized due to apikey – Hammad Shabbir Feb 29 '20 at 18:06
  • Error listing item should be removed now – Hammad Shabbir Feb 29 '20 at 18:06
  • It did remove that error but now it says "Bad authenticity token. The documentation does tell you that you have to send specific header requests but this does not seem to work. Any idea? – Real Feb 29 '20 at 18:46
  • if I would have the api key I would surely look whats the issue is after putting an apikey – Hammad Shabbir Feb 29 '20 at 18:47
  • try to use these headers `request.AddHeader("Content-Type", "application/json"); request.AddHeader("x-requested-with", "XMLHttpRequest"); request.AddParameter("application/json", "{\n \"apikey\": \"123\"\n}", ParameterType.RequestBody); ` – Hammad Shabbir Feb 29 '20 at 18:57
  • It gives me an error `wrong or unauthenticated request` which might get resolve after entering correct API – Hammad Shabbir Feb 29 '20 at 18:58
0

It looks like they're expecting you to POST rather than GET, and to pass a JSON object in the body of your POST, with "apikey" as a property on the JSON object.

Normally, in C# you would create a model class that you would then serialize for your post, but if all you have to post is the apikey I would just serialize a Dictionary object with your apikey as the only member of the collection.

For instance, I think this code might do what you want.

    private const string URL = "https://api.skinbaron.de/";
    private static string urlParameters = "?api_key=123"; // I have replaced the "123" with my apikey
    private static string apiKey = "";

    static void Main(string[] args)
    {
        using (var webClient = new WebClient()) {
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

            var postDictionary = new Dictionary<string, string>() {
                {"apikey", apiKey}
            };

            var responseBody = webClient.UploadString(URL, JsonConvert.SerializeObject(postDictionary));
        }
    }