英文:
WebClient Authorization C# x-access-token
问题
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.
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.
答案1
得分: 0
根据https://www.goldapi.io/的API文档,它需要将x-access-token添加到请求头中,但您传递的是X-ApiKey,因此您会收到403错误,这表示身份验证请求不正确。如下所示:
正确的方式:
using (WebClient webClient = new WebClient())
{
    webClient.Headers.Add("Content-Type", "application/json");
    webClient.Headers.Add("x-access-token", apikey);
    string response = webClient.DownloadString("https://www.goldapi.io/api/XAU/PLN");
    dynamic dobj = JsonConvert.DeserializeObject<dynamic>(response);
    var temp = dobj["price"];
    Console.WriteLine(response);
    return Ok(response);
}
输出:
英文:
> 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:
<!-- language: c# -->
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论