I try to get json from api (https://www.goldapi.io). It requires api key to have acces to it. I have no idea where is a problem. It always shows me 403 forbidden. It is desctop application - Windows Forms App.
public class GetData
{
private const string sourceSiteUrlGoldPLN = "https://www.goldapi.io/api/XAU/PLN";
private const string apiKey = "xxxxxxxxxxxxxxxxxxx";
public string GetGoldValueFromApi(string sourceSiteUrl = sourceSiteUrlGoldPLN)
{
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json");
//webClient.Headers["X-ApiKey"] = apiKey;
webClient.Headers.Add("X-ApiKey", apiKey);
string reponse = webClient.DownloadString(sourceSiteUrl);// <= 403 Forbidden here
dynamic dobj = JsonConvert.DeserializeObject<dynamic>(reponse);
var temp = dobj["price"];
Console.WriteLine(reponse);
return reponse;
}
}
}
I tried many combinations with Header properties but nothing works.
I try to get json from api (https://www.goldapi.io). It requires api key to have acces to it. I have no idea where is a problem. It always shows me 403 forbidden
As per https://www.goldapi.io/ API document it requires x-access-token as request Headers but you are passing X-ApiKey consequently, you are getting 403 error which means authentication request were incorrect. As you can see below:
Correct Way:
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json");
webClient.Headers.Add("x-access-token", apikey);
string reponse = webClient.DownloadString("https://www.goldapi.io/api/XAU/PLN");
dynamic dobj = JsonConvert.DeserializeObject<dynamic>(reponse);
var temp = dobj["price"];
Console.WriteLine(reponse);
return Ok(reponse);
}
Output: