ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
13.04k stars 2.31k forks source link

HTTPError: 401 Client Error: Unauthorized for url #1592

Closed jschelter closed 1 year ago

jschelter commented 1 year ago

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

eevilgeniuss commented 1 year ago

same issue here just now... python 3.10, latest yfinance version, windows...

lapis42 commented 1 year ago

It seems "v10 quoteSummary" also needs cookie and crumb.

jyerramshetty commented 1 year ago

Same here, from yesterday (12th July, 2023)...

ictheblackc commented 1 year ago

I have the same problem...

vkrishnam commented 1 year ago

Yes, witnessing the same problem.

NinoNinov commented 1 year ago

I have the same issue, it came on Thusday, on Wednesday morning my script was working and now again. Hope that can be fixed.

mhtorregrosa commented 1 year ago

I also have the same problem since this morning.

401 Client Error: Unauthorized for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/EPAM?modules=summaryProfile%2CfinancialData%2CquoteType%2CdefaultKeyStatistics%2CassetProfile%2CsummaryDetail&ssl=true

It would be great if we could find a solution. Thanks!

pbenaim commented 1 year ago

history work 👍 but not Info 👎

ValueRaider commented 1 year ago

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?

decadecity commented 1 year ago

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"}}}

ValueRaider commented 1 year ago

@decadecity I have code that can extract crumb, might help. I've never used it in requests.

import requests, re, json
def get_crumb():
    response = requests.get("https://finance.yahoo.com")
    pat = re.compile(r'window\.YAHOO\.context = ({.*?});', re.DOTALL)
    match = re.search(pat, response.text)
    if match:
        js_dict = json.loads(match.group(1))
        return js_dict.get('crumb')
    return None
decadecity commented 1 year ago

Done a bit of digging around with curl and browser requests and as a minimum it seems like it needs the A1 cookie and the crumb URL parameter.

pbenaim commented 1 year ago

I had already noticed these errors on the infos, when asking for example, for crypto currencies, like BTC-USD

lapis42 commented 1 year ago

@ValueRaider Yes, I was able to retrieve the data after providing crumb with cookie.

vismoh2010 commented 1 year ago

I cannot understand what solution has to be done. Is this solved as I am having the same problem? It says invalid crumb.

ValueRaider commented 1 year ago

@lapis42 @decadecity Could you 2 elaborate on how you obtained and provided cookie & crumb?

decadecity commented 1 year ago

I got the cookie by visiting finances.yahoo.com in a browser and clicked through the consent dialogue which sends a POST to consent.yahoo.com which returns a 302 with the Set-Cookie for the A1. I got the crumb from window.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.

ValueRaider commented 1 year ago

@decadecity With a basic free Yahoo account?

decadecity commented 1 year ago

@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.

decadecity commented 1 year ago

@ValueRaider @signifer-geo v0.2.23 is working for me 👍🏻

pbenaim commented 1 year ago

Not for me :-( Maybe value is close ???

ValueRaider commented 1 year ago

@pbenaim You're right, info is missing data. We should have tested more. Can people try Git branch hotfix/info-missing-values - looks good to me, but worth checking

lapis42 commented 1 year ago

@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

pedrochassin commented 1 year ago

I was able to fix it by upgrading yfinance: pip install --upgrade yfinance hope it works for someone

vkrishnam commented 1 year ago

Yes, upgrading to 0.2.23 works !!!

On Fri, Jul 14, 2023 at 9:55 AM pedrochassin @.***> wrote:

I was able to fix it by upgrading yfinance: pip install --upgrade yfinance hope it works for someone

— Reply to this email directly, view it on GitHub https://github.com/ranaroussi/yfinance/issues/1592#issuecomment-1635249741, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGDCBJP6UGZ4WFQFD5VUAGTXQDC3NANCNFSM6AAAAAA2ICGYHU . You are receiving this because you commented.Message ID: @.***>

farbodbahari commented 1 year ago

Is anyone able to pull the same number of fields with .23 version as they did with .22 version? I need the other fundamentals fields such as revenue and margins whcih the .23 veraion does not provide anymore.

import yfinance as yf msft = yf.Ticker("MSFT") msft.get_info()

jyerramshetty commented 1 year ago

It worked after upgradation, but some fields are missing, like "currentPrice" as well... Any idea on how to get that information?...

ValueRaider commented 1 year ago

Does no one read anymore? https://github.com/ranaroussi/yfinance/issues/1592#issuecomment-1634938667 <- click it @farbodbahari @jyerramshetty @Coachcflan @OktayBogazkaya @ColaOfficial

bot-unit commented 1 year ago

May be we should use this: params_dict = 'ssl=true' for m in modules: params_dict += f'&modules={m}'

decadecity commented 1 year ago

How's this:

import urllib.parse

modules = ['summaryProfile', 'financialData', 'quoteType',
                     'defaultKeyStatistics', 'assetProfile', 'summaryDetail']
urllib.parse.urlencode([('ssl', 'true')] + [('module', m) for m in modules])

Results in:

'ssl=true&module=summaryProfile&module=financialData&module=quoteType&module=defaultKeyStatistics&module=assetProfile&module=summaryDetail'

emmaai commented 1 year ago

I've fixed it. Can I have permission to push to the repo? thanks.

ValueRaider commented 1 year ago

Why are people fixing this instead of reading the thread? I've already fixed it, just waiting for someone to verify.

Am I muted?

emmaai commented 1 year ago

Why are people fixing this instead of reading the thread? I've already fixed it, just waiting for someone to verify.

Am I muted?

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

ValueRaider commented 1 year ago

@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.

decadecity commented 1 year ago

@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:

>>> import yfinance as yf
>>> yf.Ticker('AAPL').info
{'address1': 'One Apple Park Way', 'city': 'Cupertino', 'state': 'CA', 'zip': '95014', 'country': 'United States', 'phone': '408 996 1010', 'website': 'https://www.apple.com', 'industry': 'Consumer Electronics', 'industryDisp': 'Consumer Electronics', 'sector': 'Technology', 'longBusinessSummary': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. The company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, and HomePod. It also provides AppleCare support and cloud services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. In addition, the company offers various services, such as Apple Arcade, a game subscription service; Apple Fitness+, a personalized fitness service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was incorporated in 1977 and is headquartered in Cupertino, California.', 'fullTimeEmployees': 164000, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Timothy D. Cook', 'age': 61, 'title': 'CEO & Director', 'yearBorn': 1961, 'fiscalYear': 2022, 'totalPay': 16425933, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Luca  Maestri', 'age': 59, 'title': 'CFO & Sr. VP', 'yearBorn': 1963, 'fiscalYear': 2022, 'totalPay': 5019783, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Jeffrey E. Williams', 'age': 58, 'title': 'Chief Operating Officer', 'yearBorn': 1964, 'fiscalYear': 2022, 'totalPay': 5018337, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Ms. Katherine L. Adams', 'age': 58, 'title': 'Sr. VP, Gen. Counsel & Sec.', 'yearBorn': 1964, 'fiscalYear': 2022, 'totalPay': 5015208, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': "Ms. Deirdre  O'Brien", 'age': 55, 'title': 'Sr. VP of Retail', 'yearBorn': 1967, 'fiscalYear': 2022, 'totalPay': 5019783, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Chris  Kondo', 'title': 'Sr. Director of Corp. Accounting', 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. James  Wilson', 'title': 'Chief Technology Officer', 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Ms. Mary  Demby', 'title': 'Chief Information Officer', 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Ms. Nancy  Paxton', 'title': 'Sr. Director of Investor Relations & Treasury', 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Greg  Joswiak', 'title': 'Sr. VP of Worldwide Marketing', 'exercisedValue': 0, 'unexercisedValue': 0}], 'auditRisk': 4, 'boardRisk': 1, 'compensationRisk': 5, 'shareHolderRightsRisk': 1, 'overallRisk': 1, 'governanceEpochDate': 1688169600, 'compensationAsOfEpochDate': 1672444800, 'maxAge': 86400, 'priceHint': 2, 'previousClose': 190.54, 'open': 190.23, 'dayLow': 190.1, 'dayHigh': 191.175, 'regularMarketPreviousClose': 190.54, 'regularMarketOpen': 190.23, 'regularMarketDayLow': 190.1, 'regularMarketDayHigh': 191.175, 'dividendRate': 0.96, 'dividendYield': 0.0050999997, 'exDividendDate': 1683849600, 'payoutRatio': 0.1559, 'fiveYearAvgDividendYield': 0.89, 'beta': 1.292349, 'trailingPE': 32.227657, 'forwardPE': 29.044073, 'volume': 8928348, 'regularMarketVolume': 8928348, 'averageVolume': 56677663, 'averageVolume10days': 51027070, 'averageDailyVolume10Day': 51027070, 'bid': 190.73, 'ask': 190.74, 'bidSize': 2200, 'askSize': 900, 'marketCap': 3005911924736, 'fiftyTwoWeekLow': 124.17, 'fiftyTwoWeekHigh': 194.48, 'priceToSalesTrailing12Months': 7.8056374, 'fiftyDayAverage': 180.4268, 'twoHundredDayAverage': 156.0571, 'trailingAnnualDividendRate': 0.92, 'trailingAnnualDividendYield': 0.0048283827, 'currency': 'USD', 'enterpriseValue': 3038578737152, 'profitMargins': 0.24493, 'floatShares': 15711872289, 'sharesOutstanding': 15728700416, 'sharesShort': 117870227, 'sharesShortPriorMonth': 117654782, 'sharesShortPreviousMonthDate': 1685491200, 'dateShortInterest': 1688083200, 'sharesPercentSharesOut': 0.0075, 'heldPercentInsiders': 0.00071000005, 'heldPercentInstitutions': 0.61232, 'shortRatio': 1.87, 'shortPercentOfFloat': 0.0075, 'impliedSharesOutstanding': 15728700416, 'bookValue': 3.953, 'priceToBook': 48.34556, 'lastFiscalYearEnd': 1663977600, 'nextFiscalYearEnd': 1695513600, 'mostRecentQuarter': 1680307200, 'earningsQuarterlyGrowth': -0.034, 'netIncomeToCommon': 94321000448, 'trailingEps': 5.93, 'forwardEps': 6.58, 'pegRatio': 4.0, 'lastSplitFactor': '4:1', 'lastSplitDate': 1598832000, 'enterpriseToRevenue': 7.89, 'enterpriseToEbitda': 24.547, '52WeekChange': 26.882862, 'SandP52WeekChange': 16.74484, 'lastDividendValue': 0.24, 'lastDividendDate': 1683849600, 'exchange': 'NMS', 'quoteType': 'EQUITY', 'symbol': 'AAPL', 'underlyingSymbol': 'AAPL', 'shortName': 'Apple Inc.', 'longName': 'Apple Inc.', 'firstTradeDateEpochUtc': 345479400, 'timeZoneFullName': 'America/New_York', 'timeZoneShortName': 'EDT', 'uuid': '8b10e4ae-9eeb-3684-921a-9ab27e4d87aa', 'messageBoardId': 'finmb_24937', 'gmtOffSetMilliseconds': -14400000, 'currentPrice': 191.11, 'targetHighPrice': 240.0, 'targetLowPrice': 140.0, 'targetMeanPrice': 188.98, 'targetMedianPrice': 190.0, 'recommendationMean': 2.0, 'recommendationKey': 'buy', 'numberOfAnalystOpinions': 39, 'totalCash': 55872000000, 'totalCashPerShare': 3.552, 'ebitda': 123788001280, 'totalDebt': 109614997504, 'quickRatio': 0.764, 'currentRatio': 0.94, 'totalRevenue': 385095008256, 'debtToEquity': 176.349, 'revenuePerShare': 24.116, 'returnOnAssets': 0.20559, 'returnOnEquity': 1.4560499, 'grossProfits': 170782000000, 'freeCashflow': 83796623360, 'operatingCashflow': 109583998976, 'earningsGrowth': 0.0, 'revenueGrowth': -0.025, 'grossMargins': 0.43181, 'ebitdaMargins': 0.32145, 'operatingMargins': 0.29163, 'financialCurrency': 'USD', 'trailingPegRatio': 2.618}
arturmeneghel commented 1 year ago

Hello all, I do not seem to understand how to implement the fix suggested in https://github.com/ranaroussi/yfinance/compare/main...hotfix/info-missing-values, could someone explain?

ValueRaider commented 1 year ago

@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

decadecity commented 1 year ago

@ValueRaider on main (0.2.23) it returns 37, it returned 129 on the fix branch so, purely based on number of keys, seems like it's working.

farbodbahari commented 1 year ago

The 23 version does not work anymore. I now get error with 24 version - which was uploaded an hour ago- as well.

NinoNinov commented 1 year ago

What error I extracted sucessfully data that needed for my report with version 24

tusharrusia commented 1 year ago

What error I extracted sucessfully data that needed for my report with version 24

Working fine for me also, tested just now.

meekey463 commented 1 year ago

I was able to fix it by upgrading yfinance: pip install --upgrade yfinance hope it works for someone

updating yfinance worked.thanks

hzahiri1985 commented 1 year ago

it works for me by upgrading the finance to 0.2.24. thanks everyone!

VaglioF commented 1 year ago

I was able to fix it by upgrading yfinance: pip install --upgrade yfinance hope it works for someone

It works, thank you!

ictheblackc commented 1 year ago

version 0.2.24 works fine!