I am writing a demo app for myself to learn and suddenly I started getting 403 when doing api.stackexchange.com requests and I fail at EnsureSuccessStatusCode(). I'm doing them to populate my database with some tags data. (injected client just sets AutomaticDecompression to GZip)
public async Task<List<TagsImport>> ImportStackOverflowTagsAsync()
{
var tagImportsList = new List<TagsImport>();
var httpClient = _httpClient.GetClient();
httpClient.BaseAddress = new Uri("https://api.stackexchange.com/");
httpClient.DefaultRequestHeaders.Accept.Clear();
for (var i = 1; i <= 10; i++)
{
var response = await httpClient.GetAsync($"2.3/tags?page={i}&pagesize=100&order=desc&sort=popular&site=stackoverflow");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var tags = JsonSerializer.Deserialize<TagsImport>(responseBody);
tagImportsList.Add(tags);
}
return tagImportsList;
}
Tried manually testing the request and then testing previous versions of the app, the test in the app just returns "System.Net.Http.HttpRequestException : Response status code does not indicate success: 403 (Forbidden)." while manually writing "https://api.stackexchange.com/2.3/tags?page=1&pagesize=100&order=desc&sort=popular&site=stackoverflow" in browser returns the data properly.
Am I missing something to make it work?
backofffield (when it is not absent)? Try adding a small delay between the requests you make.has_moreProperty of the resulting JSON, to verify that you have more pages to get. Maybe, instead of a loop, increment a counter and bail out either ifhas_more = falseor you have reached your goal. Add aTask.Delay(100)between calls toGetAsync()(orGetStringAsync()orGetFromJsonAsync<T>())