Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlurlHttpException with StatusCode: 429, ReasonPhrase: 'Too Many Requests' #68

Open
irigopou opened this issue Mar 6, 2024 · 10 comments

Comments

@irigopou
Copy link

irigopou commented Mar 6, 2024

Since last night, prices cannot be retrieved from Yahoo Finance.
The failure occurs in Yahoo - Quote.cs in the following method

public async Task<IReadOnlyDictionary<string, Security>> QueryAsync(CancellationToken token = default)

Below, the FlurlHttpException ex is caught.

            try
            {
                data = await url
                    .WithCookie(YahooSession.Cookie.Name, YahooSession.Cookie.Value)
                    .GetAsync(token)
                    .ReceiveJson()
                    .ConfigureAwait(false);
            }
            catch (FlurlHttpException ex)

The message in ex contains:

ResponseMessage = {StatusCode: 429, ReasonPhrase: 'Too Many Requests'
@BobMc98
Copy link

BobMc98 commented Mar 6, 2024

Can confirm the same. Possible rate limits on the part of Yahoo, but why all of a sudden.

@irigopou
Copy link
Author

irigopou commented Mar 6, 2024

Rate limit? Definitely not, as the exemption was thrown immediately after the first request.

@kamoco
Copy link

kamoco commented Mar 6, 2024

I am hitting the same too.

@gyantal
Copy link

gyantal commented Mar 6, 2024

I have found a way. Tip came from here:
https://stackoverflow.com/questions/78111453/yahoo-finance-api-file-get-contents-429-too-many-requests

Do the same thing as in InitAsync() when we get the crumb:
https://github.com/karlwancl/YahooFinanceApi/blob/master/YahooFinanceApi/YahooSession.cs
Which was:

const string userAgentKey = "User-Agent";
const string userAgentValue = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
...
.WithHeader(userAgentKey, userAgentValue)
.GetAsync()

So, both in Historical.cs and in Quote.cs, put this line before the GetAsync()

.WithHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36")
.GetAsync(...

Works for me at the moment. (from the UK).
Somebody could clean the quick-and-dirty solution and create a PR to fix it properly in the repo.

One proper solution is to create the string userAgentKey/userAgentValue as data member fields somewhere, and all 3 GetAsync() could use that.

sergemat added a commit to sergemat/YahooFinanceApi that referenced this issue Mar 6, 2024
sergemat added a commit that referenced this issue Mar 6, 2024
#68 Fixed issue with missing and outdated HTTP Header
@irigopou
Copy link
Author

irigopou commented Mar 6, 2024

Feeds work again now, even without the change proposed above!

@gyantal
Copy link

gyantal commented Mar 6, 2024

Thx for checking.

Maybe it works for you now without the proposed changes, but you might use it from a different country (USA?). I am from London.
And I just checked it again right now. 5 minutes ago.
For me, this line is still needed at this moment. Otherwise I get the Furl exception.
.WithHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36")

Let's hear the experiences of others.

@irigopou
Copy link
Author

irigopou commented Mar 6, 2024

I am in Germany. It started failing last night, but it worked again about an hour ago.

@sergemat
Copy link
Collaborator

sergemat commented Mar 6, 2024

I made code changes as suggested. Please try it.
It works from USA today with and without the changes.
However, there are random 401 errors are occurring during the testing.

@gyantal
Copy link

gyantal commented Mar 6, 2024

Funny enough. 30min after my previous post (that I double-checked), now in the UK, I get the same as you:
It started working with or without the changes. Hmmm.

I also have the random 'Call failed with status code 401 (Unauthorized)' when we get the Crumb.
But usually, it works after the second try.

Thx for sharing the experience.

@dotnetshadow
Copy link

In AU I too get some random 401
Call failed with status code 401 (Unauthorized): GET https://query1.finance.yahoo.com/v1/test/getcrumb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants