finance-go icon indicating copy to clipboard operation
finance-go copied to clipboard

YFinance changed their api's (or their security)

Open TobiasFP opened this issue 2 years ago • 32 comments

Hi. There seams to be an issue with the finance-go, since yFinance changed its encryption keys. Apparantly they change their encryption keys every day or even sometimes more than once a day. This has lead to finance-go not working for a while. yFinance has the same issue: https://github.com/ranaroussi/yfinance/issues/1407

I just wanted to bring it to your attention. I have been very happy with this library, so thank you very much for your work.

TobiasFP avatar Feb 23 '23 20:02 TobiasFP

v6 of the API seems to work, not 7.

ktpx avatar May 05 '23 11:05 ktpx

@ktpx You saved me! :)

md-dev-lab avatar May 07 '23 10:05 md-dev-lab

I think they pulled the plug on v6 now. :(

ktpx avatar May 24 '23 16:05 ktpx

All projects I see using the Yahoo API are facing this. There is this nugget I got from the ticker issue report on the subject:

https://stackoverflow.com/questions/76065035/yahoo-finance-v7-api-now-requiring-cookies-python

Not sure how/if that helps...

joce avatar May 25 '23 17:05 joce

Saw somewhere that for instance, an url like this: https://query2.finance.yahoo.com/v6/finance/quoteSummary/AAPL?modules=financialData is working, but I'm not sure if the data structures are the same. Is someone working on a patch?

TiagoNH avatar May 26 '23 18:05 TiagoNH

https://github.com/achannarasappa/ticker/issues/250#issuecomment-1563423747 Looks like we're out of luck. 😢

joce avatar May 26 '23 19:05 joce

Saw somewhere that for instance, an url like this: https://query2.finance.yahoo.com/v6/finance/quoteSummary/AAPL?modules=financialData is working, but I'm not sure if the data structures are the same. Is someone working on a patch?

Totally different structure than the quotes. This show financial data. But atleast one can grab the currentprices.

ktpx avatar May 28 '23 16:05 ktpx

Crumb is a param that is easy to gain, not sure how it will behave in long term.

For now, it is possible to generate crumb via https://query2.finance.yahoo.com/v1/test/getcrumb and then pass crumb value as param https://query2.finance.yahoo.com/v7/finance/quote?symbols=AAPL&crumb=abcdef.g. (Works with v7 only)

I have had the same crumb for a few days now, so it's possible to cache it locally on disk to avoid this call.

LandRover avatar Jun 05 '23 13:06 LandRover

If you inspect the cookie header, it says one year. "Expires=Wed, 5 Jun 2024 22:54:09 GMT"

ktpx avatar Jun 06 '23 16:06 ktpx

Hi there, is there any workaround I could use to test ? This is broken for a while.

rosimildo avatar Jun 12 '23 11:06 rosimildo

So I have a fix for this and I have opened a PR for it. Not sure who should be reviewing it. I also have "modernized" the finance-mock project used to test finance-go. PR can be found here: https://github.com/piquette/finance-mock/pull/1

joce avatar Jun 29 '23 16:06 joce

I would love to review it, but i don't think we can get it in as i think the project is dead here. Maybe fork?

TobiasFP avatar Jun 29 '23 16:06 TobiasFP

Is it, though? There were a couple of PR that were merged a month ago by @ackleymi .

joce avatar Jun 29 '23 16:06 joce

My bad. Just add me to the pr if you want

TobiasFP avatar Jun 29 '23 17:06 TobiasFP

Doesn't look like I can add you, but feel free to head over the the PR and comment on it: https://github.com/piquette/finance-go/pull/28

joce avatar Jun 29 '23 17:06 joce

Doesn't look like I can add you, but feel free to head over the the PR and comment on it: https://github.com/piquette/finance-go/pull/28

Done. Good job!

TobiasFP avatar Jul 11 '23 05:07 TobiasFP

Did they add some kind of rate limit? I don't get a crumb anymore, just "Too many requests".

ktpx avatar Jul 20 '23 19:07 ktpx

I don't have access to a machine with the library on it atm. I'll check about this later today.

On Thu, Jul 20, 2023 at 3:32 PM ktpx @.***> wrote:

Did they add some kind of rate limit? I don't get a crumb anymore, just "Too many requests".

— Reply to this email directly, view it on GitHub https://github.com/piquette/finance-go/issues/25#issuecomment-1644483451, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2NOHUSYZVJRSGVCHALXRGBTHANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.***>

joce avatar Jul 20 '23 20:07 joce

I don't have access to a machine with the library on it atm. I'll check about this later today. On Thu, Jul 20, 2023 at 3:32 PM ktpx @.> wrote: Did they add some kind of rate limit? I don't get a crumb anymore, just "Too many requests". — Reply to this email directly, view it on GitHub <#25 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2NOHUSYZVJRSGVCHALXRGBTHANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.>

Not a software issue really, API.

curl  https://query2.finance.yahoo.com/v1/test/getcrumb
Too Many Requests

Thats atleast the URL i used in my progs

ktpx avatar Jul 20 '23 20:07 ktpx

You need to have a cookie set when you call getcrumb. Just calling to it from curl won't work.

On Thu, Jul 20, 2023 at 4:19 PM ktpx @.***> wrote:

I don't have access to a machine with the library on it atm. I'll check about this later today. … <#m_9094901114993369745_> On Thu, Jul 20, 2023 at 3:32 PM ktpx @.> wrote: Did they add some kind of rate limit? I don't get a crumb anymore, just "Too many requests". — Reply to this email directly, view it on GitHub <#25 (comment) https://github.com/piquette/finance-go/issues/25#issuecomment-1644483451>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2NOHUSYZVJRSGVCHALXRGBTHANCNFSM6AAAAAAVGDRPD4 https://github.com/notifications/unsubscribe-auth/AAATT2NOHUSYZVJRSGVCHALXRGBTHANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.>

Not a software issue really, API.

curl https://query2.finance.yahoo.com/v1/test/getcrumb Too Many Requests

— Reply to this email directly, view it on GitHub https://github.com/piquette/finance-go/issues/25#issuecomment-1644548867, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2M2VWMKEK3GUPQ4VOTXRGHG7ANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.***>

joce avatar Jul 21 '23 14:07 joce

I do set cookie in the code, curl was bad example. But I still get same error. It worked, and literary didnt work half hour later, no code changed, so gotta be something yahoo changed.

ktpx avatar Jul 21 '23 14:07 ktpx

Hum. I don't know what to say.

I just ran the "examples" program (examples/main.go) from my branch, and everything works nicely.

On Fri, Jul 21, 2023 at 10:54 AM ktpx @.***> wrote:

I do set cookie in the code, curl was bad example. But I still get same error.

— Reply to this email directly, view it on GitHub https://github.com/piquette/finance-go/issues/25#issuecomment-1645718376, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2KW72BSDMITUYRSZTLXRKJZNANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.***>

joce avatar Jul 21 '23 14:07 joce

Which site do you query to get the cookie? Seems might be the problem. Ive used fc.yahoo.com, as its the only one that worked for me. For regular api calls, query2.

ktpx avatar Jul 21 '23 14:07 ktpx

 cookieURL = "https://login.yahoo.com"
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0)

Gecko/20100101 Firefox/113.0"

func fetchCookies() (string, time.Time, error) { client := http.Client{} request, err := http.NewRequest("GET", cookieURL, nil) if err != nil { return "", time.Time{}, err }

request.Header = http.Header{
    "Accept":                   {"*/*"},
    "Accept-Encoding":          {"gzip, deflate, br"},
    "Accept-Language":          {"en-US,en;q=0.5"},
    "Connection":               {"keep-alive"},
    "Host":                     {"login.yahoo.com"},
    "Sec-Fetch-Dest":           {"document"},
    "Sec-Fetch-Mode":           {"navigate"},
    "Sec-Fetch-Site":           {"none"},
    "Sec-Fetch-User":           {"?1"},
    "TE":                       {"trailers"},
    "Update-Insecure-Requests": {"1"},
    "User-Agent":               {userAgent},
}

func fetchCrumb(cookies string) (string, error) { client := http.Client{} request, err := http.NewRequest("GET", crumbURL, nil) if err != nil { return "", err }

request.Header = http.Header{
    "Accept":          {"*/*"},
    "Accept-Encoding": {"gzip, deflate, br"},
    "Accept-Language": {"en-US,en;q=0.5"},
    "Connection":      {"keep-alive"},
    "Content-Type":    {"text/plain"},
    "Cookie":          {cookies},
    "Host":            {"query1.finance.yahoo.com"},
    "Sec-Fetch-Dest":  {"empty"},
    "Sec-Fetch-Mode":  {"cors"},
    "Sec-Fetch-Site":  {"same-site"},
    "TE":              {"trailers"},
    "User-Agent":      {userAgent},
}

On Fri, Jul 21, 2023 at 10:58 AM ktpx @.***> wrote:

Which site do you query to get the cookie? Seems might be the problem. Ive used fc.yahoo.com, as its the only one that worked for me. For regular api calls, query2.

— Reply to this email directly, view it on GitHub https://github.com/piquette/finance-go/issues/25#issuecomment-1645724393, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATT2ON5ODH36WILOHXD6DXRKKKFANCNFSM6AAAAAAVGDRPD4 . You are receiving this because you commented.Message ID: @.***>

joce avatar Jul 21 '23 15:07 joce

Ok using login subdomain again got me the cookie, but still no crum. Mind showing your crumb URL.....? I didnt find it in the source

ktpx avatar Jul 21 '23 15:07 ktpx

I will refer you to my pull request:

https://github.com/piquette/finance-go/pull/28

You should be able to get everything you need there.

joce avatar Jul 21 '23 15:07 joce

Thanks, its same url. Im guessing they are doing some IP based limiting, or blocking, im just getting the too many requests from here (EU).

ktpx avatar Jul 21 '23 15:07 ktpx

Have you sync'd my branch and run the examples program?

joce avatar Jul 21 '23 15:07 joce

Ok, confirmed is a location issue. So ill assume people using this tool wil also have problem if aren't in US.

ktpx avatar Jul 21 '23 20:07 ktpx

So, that means this solution is only working if the request is made in the US?

TiagoNH avatar Aug 08 '23 13:08 TiagoNH