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

HTTPError: 401 Client Error: Unauthorized for url #1592

Closed
jschelter opened this issue Jul 12, 2023 · 45 comments · Fixed by #1603
Closed

HTTPError: 401 Client Error: Unauthorized for url #1592

jschelter opened this issue Jul 12, 2023 · 45 comments · Fixed by #1603

Comments

@jschelter
Copy link

jschelter commented Jul 12, 2023

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
Copy link

eevilgeniuss commented Jul 12, 2023

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

@lapis42
Copy link

lapis42 commented Jul 13, 2023

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

@jyerramshetty
Copy link

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

@ictheblackc
Copy link

I have the same problem...

@vkrishnam
Copy link

Yes, witnessing the same problem.

@NinoNinov
Copy link

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
Copy link

mhtorregrosa commented Jul 13, 2023

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
Copy link

pbenaim commented Jul 13, 2023

history work 👍 but not Info 👎

@ValueRaider
Copy link
Collaborator

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
Copy link

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
Copy link
Collaborator

ValueRaider commented Jul 13, 2023

@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

@ValueRaider ValueRaider pinned this issue Jul 13, 2023
@decadecity
Copy link

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
Copy link

pbenaim commented Jul 13, 2023

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

@lapis42
Copy link

lapis42 commented Jul 13, 2023

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

@vismoh2010
Copy link

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

@ValueRaider
Copy link
Collaborator

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

@decadecity
Copy link

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
Copy link
Collaborator

@decadecity With a basic free Yahoo account?

@decadecity
Copy link

@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
Copy link

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

@pbenaim
Copy link

pbenaim commented Jul 13, 2023

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

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 13, 2023

@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

ValueRaider referenced this issue Jul 13, 2023
dead code deleted
@lapis42
Copy link

lapis42 commented Jul 14, 2023

@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
Copy link

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

@vkrishnam
Copy link

vkrishnam commented Jul 14, 2023 via email

@farbodbahari
Copy link

farbodbahari commented Jul 14, 2023

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()

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 14, 2023

Does no one read anymore? #1592 (comment) <- click it
@farbodbahari @jyerramshetty @Coachcflan @OktayBogazkaya @ColaOfficial

Repository owner deleted a comment from OktayBogazkaya Jul 14, 2023
Repository owner deleted a comment from Coachcflan Jul 14, 2023
Repository owner deleted a comment from farbodbahari Jul 14, 2023
Repository owner deleted a comment from ColaOfficial Jul 14, 2023
@bot-unit
Copy link

bot-unit commented Jul 14, 2023

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

@decadecity
Copy link

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
Copy link

emmaai commented Jul 14, 2023

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

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 14, 2023

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
Copy link

emmaai commented Jul 14, 2023

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...
#1592 (comment) from @decadecity is the right answer
and it took only one line fix
params_dict["modules"] = modules

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 14, 2023

@emmaai My branch does that, see 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
Copy link

@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
Copy link

Hello all,
I do not seem to understand how to implement the fix suggested in main...hotfix/info-missing-values, could someone explain?

Repository owner deleted a comment from eromualdez Jul 14, 2023
@ValueRaider
Copy link
Collaborator

ValueRaider commented Jul 14, 2023

@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
Copy link

@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
Copy link

farbodbahari commented Jul 14, 2023

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

@NinoNinov
Copy link

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

@tusharrusia
Copy link

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

Working fine for me also, tested just now.

@meekey463
Copy link

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

updating yfinance worked.thanks

@hzahiri1985
Copy link

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

@VaglioF
Copy link

VaglioF commented Jul 15, 2023

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

It works, thank you!

@ictheblackc
Copy link

version 0.2.24 works fine!

Repository owner locked as resolved and limited conversation to collaborators Jul 15, 2023
@ValueRaider ValueRaider unpinned this issue Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.