yahoofinance-api: Intermittent HTTP 401 returned from Yahoo API
I am calling the API at a frequency of 5 minutes for around 10 stocks, which worked fine for quite some time.
Since a few days, the Yahoo API still works, but sometimes fails with HTTP 401.
There have previously been issues with some cookies which lead to this error, e.g. https://github.com/sstrickx/yahoofinance-api/issues/130
Is this a re-occurrence of this?
If the API now really would need authentication via a Yahoo-ID and OAuth it would fail always, not intermittently, so seems at least some similar issue.
Can I investigate this some more? Logging? Debugging?
Code:
Stock stock = YahooFinance.get("ORCL");
Exception
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: https://query1.finance.yahoo.com/v7/finance/quote?symbols=ORCL
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1974)
at java.base/sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1969)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1968)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at yahoofinance.quotes.query1v7.QuotesRequest.getResult(QuotesRequest.java:74)
at yahoofinance.YahooFinance.getQuotes(YahooFinance.java:381)
at yahoofinance.YahooFinance.get(YahooFinance.java:98)
at yahoofinance.YahooFinance.get(YahooFinance.java:82)
...
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://query1.finance.yahoo.com/v7/finance/quote?symbols=ORCL
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1924)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1520)
at java.base/[java.net](http://java.net/).HttpURLConnection.getResponseCode(HttpURLConnection.java:527)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:334)
at yahoofinance.util.RedirectableRequest.openConnection(RedirectableRequest.java:54)
at yahoofinance.util.RedirectableRequest.openConnection(RedirectableRequest.java:34)
at yahoofinance.quotes.query1v7.QuotesRequest.getResult(QuotesRequest.java:72)
... 8 more
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 5
- Comments: 42
Isn’t this valid?
This means the following may be a workaround in the client-code without changes to the yahoofinance-api code itself
At least it seems to make it work for me.
It seems the “v6” API was now shutdown by Yahoo, likely because everyone switched over from “v7”
I have the same problem - Crumb is indeed generated on the browser, but not in the app where the same URL is used. Like I’ve mentioned before, the patterns are not matched against the HTML the app receives, so scraping the crumb and cookie isn’t working for me also.
Changing the URL does seem to work. Apparently that one hasn’t changed how it’s accessed and is working exactly as it was before (for now, anyway!)
@centic9 @emidesy Same here. Apparently you need to pass the crumb as an argument now?
If you go to https://query1.finance.yahoo.com/v1/test/getcrumb on a browser, you should get a string result. If you pass it to the URL, you should be able to get the info like so:
https://query1.finance.yahoo.com/v7/finance/quote?symbols=ORCL&crumb=<YOUR_CRUMB_RESULT>However, this is not working in my Android App. I’ve debugged the app a little and Cookie and Crumb are returning empty because the
CrumbManageris not matching the following patterns that are used:I’m assuming something changed related to how the API is accessed and the content returned from
HISTQUOTES2_SCRAPE_URLalso changed.When I tried to load https://query1.finance.yahoo.com/v7/finance/quote?symbols=ORCL in the Browser, it mostly works, but sometimes I get
So it seems this may be some problem at Yahoo itself.
That’s the Interactive Brokers API. You don’t need that.
Is there a reason that this still hasn’t been added to the repo?
You need to use the v7 quotes query URL as the v6 one has been disabled.
Looking at the the 3.17.0 code base you need to change the getResult() method in the abstract class QuotesRequest in package yahoofinance.quotes.query1v7 and append the crumb to the URL.
@kallavikas
I’ve adapted the code to my use case, and I can confirm this solutions works (for now). Let’s hope it stays that way this time.
I’m assuming that the content from which the crumb is extracted is regionalized then. I’m from Europe and the content from the base URL scrape does not get any pattern matches. Let’s hope v6 doesn’t suffer the same fate!
Hi,
I tried to use windscribe vpn. I found out that it did not work from Europe(Vienna, Oslo, Zagreb) but it worked from Tokyo and US. Then I tried also Frankfurt Germany and it worked. After that also other EU cities worked (Vienna, Oslo, Zagreb).
Change in QuotesRequest works for me. String crumb = CrumbManager.getCrumb(); params.put(“crumb”, crumb);
Thanks
Thank you @lumism! You are right, adding the crumb to the URL fixed it for me indeed on PC.
The pattern doesn’t match for me either but for some reason I can get the crumb without the cookie.