json C# httpclient authorize header不适合我?

t3irkdon  于 2024-01-09  发布在  C#
关注(0)|答案(1)|浏览(241)

我想做一个应用程序,让我从discogs查询数据库。
根据API文档,我只需要一个令牌就可以完成这项工作,所以我注册并获得了一个用户令牌。
现在,当我使用postman和https://api.discogs.com/database/search?release_title=nevermind&artist=nirvana&per_page=3&page=1&token=<my_user_token>时,我收到了我所期望的JSON。
但是当我在C#中使用token创建httpclient时:

  1. public string token = <my_user_token>;
  2. public static HttpClient client { get; set; }
  3. public static async Task InitilizeClient()
  4. {
  5. await GetAccesToken();
  6. }
  7. private static async Task GetAccesToken()
  8. {
  9. client = new HttpClient();
  10. client.DefaultRequestHeaders.Accept.Clear();
  11. client.BaseAddress = new Uri(@"https://api.discogs.com");
  12. //client.DefaultRequestHeaders.Authorization=new AuthenticationHeaderValue("Discogs", "token="+token);
  13. client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization","Discogs token=" + token);
  14. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  15. }

字符串
然后像这样使用客户端

  1. public static async Task QueryDataBaseAsync(string query)
  2. {
  3. if (query == null)
  4. {
  5. throw new Exception("query is empty");
  6. }
  7. string url = "";
  8. url = @"https://api.discogs.com/database/search?release_title="+query;
  9. if (client == null)
  10. {
  11. await InitilizeClient();
  12. }
  13. using (HttpResponseMessage response = await client.GetAsync(url))
  14. {
  15. if (response.IsSuccessStatusCode)
  16. {
  17. }
  18. else
  19. {
  20. throw new Exception(response.ReasonPhrase + " \n" + response.RequestMessage.ToString());
  21. }
  22. }
  23. }


我总是得到:
原因短语“禁止”,“状态代码:403”
当我在我的HttpResponseMessage response上设置一个断点时,我可以看到在“headers”=>“responsemessage”=>“headers”=>“authorization”下面有我的token。
我做错了什么?
我是编程新手,如果你能解释我做错了什么,我将不胜感激

xzlaal3s

xzlaal3s1#

您可能需要在标题中提供user-agent。沿着这些行:

  1. client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");

字符串
就像这样:

  1. public class DiscConsumer
  2. {
  3. //https://www.discogs.com/developers#page:authentication,header:authentication-discogs-auth-flow
  4. //curl "https://api.discogs.com/database/search?q=Nirvana" -H "Authorization: Discogs key=foo123, secret=bar456"
  5. private const string _urlQuery = "https://api.discogs.com/database/search?q={query}";
  6. private const string _key = "<....your key....>";
  7. private const string _secret = "<....your secret...>";
  8. private System.Net.Http.HttpClient _httpClient;
  9. public async Task InitilizeClient()
  10. {
  11. //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
  12. var sslhandler = new HttpClientHandler()
  13. {
  14. //...in System.Security.Authentication.SslProtocols
  15. SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
  16. };
  17. _httpClient = new System.Net.Http.HttpClient(sslhandler);
  18. string authorization = $"Discogs key={_key}, secret={_secret}";
  19. _httpClient.DefaultRequestHeaders.Add("Authorization", authorization);
  20. _httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36");
  21. }
  22. public async Task QueryDataBaseAsync(string query)
  23. {
  24. if (String.IsNullOrWhiteSpace( query ))
  25. {
  26. throw new Exception("query is empty");
  27. }
  28. string url = "";
  29. url = _urlQuery.Replace("{query}", query);
  30. if (_httpClient == null)
  31. {
  32. await InitilizeClient();
  33. }
  34. using (HttpResponseMessage response = await _httpClient.GetAsync(url))
  35. {
  36. if (response.IsSuccessStatusCode)
  37. {
  38. string s = await response.Content.ReadAsStringAsync();
  39. Console.WriteLine(s);
  40. }
  41. else
  42. {
  43. throw new Exception(response.ReasonPhrase + " \n" + response.RequestMessage.ToString());
  44. }
  45. }
  46. }
  47. }


根据https://www.discogs.com/developers#page:authentication,header:authentication-discogs-auth-flow,您可以在搜索沿着为每个请求提供key+secret。

展开查看全部

相关问题