yfinance: HTTPError: 401 Client Error: Unauthorized for url
I’m on yfinance version 0.2.22 (Python 3.9.10 on macOS 12.6.3), and since today, I keep getting the following error (e.g. on calling yf.Ticker('AAPL').info
):
HTTPError: 401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/AAPL?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true
Is this a known/global issue at the moment?
@ValueRaider hijacking top post
Please only post if you have something useful
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 12
- Comments: 45
Commits related to this issue
- Update quote.py dead code deleted — committed to ranaroussi/yfinance by signifer-geo a year ago
I was able to fix it by upgrading yfinance:
pip install --upgrade yfinance
hope it works for someoneWhat error I extracted sucessfully data that needed for my report with version 24
@decadecity Your experience is uniquely interesting, because 0.2.23 should only be returning “summaryDetail” module = ~37 values instead of ~130
Anyway I’ve created a PR #1603
@ValueRaider Apologies, I misunderstood you: I thought that was your WIP branch but as there was no PR raised for it I thought you were still looking for a fix. My bad.
As the
info
dict from 0.2.23 contatines all the data I’m after I don’t know what we’re expecting to see here. I’ve run your branch locally and this is what I get:@emmaai My branch does that, see https://github.com/ranaroussi/yfinance/compare/main...hotfix/info-missing-values . And for me this appears to fix, many more values now, I just wanted confirmation in case still problems.
coz the code on the branch doesn’t work re missing info values, at least for me… https://github.com/ranaroussi/yfinance/issues/1592#issuecomment-1635869193 from @decadecity is the right answer and it took only one line fix
params_dict["modules"] = modules
Why are people fixing this instead of reading the thread? I’ve already fixed it, just waiting for someone to verify.
Am I muted?
How’s this:
Results in:
'ssl=true&module=summaryProfile&module=financialData&module=quoteType&module=defaultKeyStatistics&module=assetProfile&module=summaryDetail'
May be we should use this: params_dict = ‘ssl=true’ for m in modules: params_dict += f’&modules={m}’
@ValueRaider I used curl to check, and I was able to retrieve the data.
To get the crumb:
curl --header "Cookie: cookie_that_i_extracted_from_the_browser" https://query1.finance.yahoo.com/v1/test/getcrumb
To get the data:
curl --header "Cookie: cookie_that_i_extracted_from_the_browser" https://query1.finance.yahoo.com/v10/finance/quoteSummary?modules=price,summaryDetail&crumb=crumb_that_i_just_got
@pbenaim You’re right,
info
is missing data. We should have tested more. Can people try Git branchhotfix/info-missing-values
- looks good to me, but worth checking@ValueRaider @signifer-geo v0.2.23 is working for me 👍🏻
@ValueRaider Initially yes but once I’d used that to figure out the minimum information required I was able to do it anonymously in a private browsing window with a completely clean session.
I got the cookie by visiting
finances.yahoo.com
in a browser and clicked through the consent dialogue which sends a POST toconsent.yahoo.com
which returns a 302 with theSet-Cookie
for theA1
. I got the crumb fromwindow.YAHOO.context.crumb
(which I found from your comment).With those two I could then put them into a command line
curl
and it returned the data:curl 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/AAPL?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true&crumb={{ crumb }}' -H 'cookie: A1={{ A1 cookie }}'
I’ve not looked into how to do this in Python.
@lapis42 @decadecity Could you 2 elaborate on how you obtained and provided cookie & crumb?
@ValueRaider Yes, I was able to retrieve the data after providing crumb with cookie.
Done a bit of digging around with
curl
and browser requests and as a minimum it seems like it needs theA1
cookie and thecrumb
URL parameter.@decadecity I have code that can extract crumb, might help. I’ve never used it in requests.
If I use the URL in the original message
Using
curl
I get:{"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Cookie"}}}
If I do the same in a browser that has a logged in session to Yahoo! finance it returns:
{"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Crumb"}}}
Well obviously this is a problem. Let’s keep thread tidy and constructive now.
@lapis42 You know that because you have it working with cookie and crumb?
It seems “v10 quoteSummary” also needs cookie and crumb.