WebClient 授权 C# x-access-token

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

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.


  1. public class GetData
  2. {
  3. private const string sourceSiteUrlGoldPLN = "https://www.goldapi.io/api/XAU/PLN";
  4. private const string apiKey = "xxxxxxxxxxxxxxxxxxx";
  5. public string GetGoldValueFromApi(string sourceSiteUrl = sourceSiteUrlGoldPLN)
  6. {
  7. using (WebClient webClient = new WebClient())
  8. {
  9. webClient.Headers.Add("Content-Type", "application/json");
  10. //webClient.Headers["X-ApiKey"] = apiKey;
  11. webClient.Headers.Add("X-ApiKey", apiKey);
  12. string reponse = webClient.DownloadString(sourceSiteUrl);// <= 403 Forbidden here
  13. dynamic dobj = JsonConvert.DeserializeObject<dynamic>(reponse);
  14. var temp = dobj["price"];
  15. Console.WriteLine(reponse);
  16. return reponse;
  17. }
  18. }
  19. }

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.


  1. public class GetData
  2. {
  3. private const string sourceSiteUrlGoldPLN = &quot;https://www.goldapi.io/api/XAU/PLN&quot;;
  4. private const string apiKey = &quot;xxxxxxxxxxxxxxxxxxx&quot;;
  5. public string GetGoldValueFromApi(string sourceSiteUrl = sourceSiteUrlGoldPLN)
  6. {
  7. using (WebClient webClient = new WebClient())
  8. {
  9. webClient.Headers.Add(&quot;Content-Type&quot;, &quot;application/json&quot;);
  10. //webClient.Headers[&quot;X-ApiKey&quot;] = apiKey;
  11. webClient.Headers.Add(&quot;X-ApiKey&quot;, apiKey);
  12. string reponse = webClient.DownloadString(sourceSiteUrl);// &lt;= 403 Forbidden here
  13. dynamic dobj = JsonConvert.DeserializeObject&lt;dynamic&gt;(reponse);
  14. var temp = dobj[&quot;price&quot;];
  15. Console.WriteLine(reponse);
  16. return reponse;
  17. }
  18. }
  19. }

I tried many combinations with Header properties but nothing works.

答案1

得分: 0

根据https://www.goldapi.io/的API文档,它需要将x-access-token添加到请求头中,但您传递的是X-ApiKey,因此您会收到403错误,这表示身份验证请求不正确。如下所示:

正确的方式:

  1. using (WebClient webClient = new WebClient())
  2. {
  3. webClient.Headers.Add("Content-Type", "application/json");
  4. webClient.Headers.Add("x-access-token", apikey);
  5. string response = webClient.DownloadString("https://www.goldapi.io/api/XAU/PLN");
  6. dynamic dobj = JsonConvert.DeserializeObject<dynamic>(response);
  7. var temp = dobj["price"];
  8. Console.WriteLine(response);
  9. return Ok(response);
  10. }

输出:

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:

确定