WebClient 授权 C# x-access-token

huangapple go评论52阅读模式
英文:

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 = &quot;https://www.goldapi.io/api/XAU/PLN&quot;;
        private const string apiKey = &quot;xxxxxxxxxxxxxxxxxxx&quot;;

        public string GetGoldValueFromApi(string sourceSiteUrl = sourceSiteUrlGoldPLN)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers.Add(&quot;Content-Type&quot;, &quot;application/json&quot;);
                //webClient.Headers[&quot;X-ApiKey&quot;] = apiKey;
                webClient.Headers.Add(&quot;X-ApiKey&quot;,  apiKey);

                string reponse = webClient.DownloadString(sourceSiteUrl);//      &lt;= 403 Forbidden here
                dynamic dobj = JsonConvert.DeserializeObject&lt;dynamic&gt;(reponse);
                var temp = dobj[&quot;price&quot;];
                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);
}

输出:

WebClient 授权 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

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:

WebClient 授权 C# x-access-token

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:

WebClient 授权 C# x-access-token

huangapple
  • 本文由 发表于 2023年2月24日 07:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定