Real
Real

Reputation: 11

Calling an API with secret and key in c#

So I have worked with a Swagger API before but since there is a codegenerator for all API calls i did not really have to worry about that stuff. Now i want to use the following api https://docs.skincay.com/#introduction

public static HttpClient ApiClient { get; set; }

    public static void InitializeClient()
    {
        if (ApiHelper.ApiClient == null)
            ApiHelper.ApiClient = new HttpClient();

        ApiHelper.ApiClient.BaseAddress = new Uri("https://api.skincay.com/v1/");
        ApiHelper.ApiClient.DefaultRequestHeaders.Accept.Clear();
        ApiHelper.ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        ApiHelper.ApiClient.DefaultRequestHeaders.Add("Authorization", "Basic clientid:secret");
    }

I followed this guide: https://www.youtube.com/watch?v=aWePkE2ReGw

I just want to work with it in a normal console application but that should not be my problem. The problem is whether the header information i am giving them is right or not since i do not get any response from my calls. I find it really hard working with this documentation and i have not found anyone trying to use this api. I hope someone here can help me with this.

Upvotes: 0

Views: 1238

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236308

You should base64 encode your authorization credentials:

var bytes = Encoding.UTF8.GetBytes($"{clientId}:{secret});
var base64 = Convert.ToBase64String(bytes);
ApiClient.DefaultRequestHeaders.Add("Authorization", $"Basic {base64}");

Note: don't forget to replace values from the tutorial with your real uri, clientId and secret

ApiClient.BaseAddress = new Uri("https://docs.skincay.com/#introduction");
string clientId = "bob";
string secret = "derparole";

Upvotes: 1

Related Questions