gadicc / node-yahoo-finance2

Unofficial API for Yahoo Finance
https://www.npmjs.com/package/yahoo-finance2
MIT License
353 stars 58 forks source link

Error fetching stock price for AAPL: Error: Failed to get crumb, status 403, statusText: Forbidden #784

Open minsixhao opened 1 week ago

minsixhao commented 1 week ago

Bug Report

I was very surprised to discover this project and then started using it. After downloading the package, I began running the test code and found errors, which made me very sad.

How do I solve this problem?

Vision

"yahoo-finance2": "^2.11.3"

Code

import yahooFinance from 'yahoo-finance2';

async function getStockPrice(symbol) {
  try {
    console.log(symbol);
    const quote = await yahooFinance.quoteSummary(symbol);
    console.log(quote);
  } catch (error) {
    console.error(`Error fetching stock price for ${symbol}:`, error);
  }
}
getStockPrice('AAPL');

Console Output:


AAPL
Please consider completing the survey at https://bit.ly/yahoo-finance-api-feedback if you haven't already; for more info see https://github.com/gadicc/node-yahoo-finance2/issues/764#issuecomment-2056623851.
Fetching crumb and cookies from https://finance.yahoo.com/quote/AAPL...
Success. Cookie expires on Sun Jun 22 2025 01:34:13 GMT+0800 (中国标准时间)
fetch https://query1.finance.yahoo.com/v1/test/getcrumb
Error fetching stock price for AAPL: Error: Failed to get crumb, status 403, statusText: Forbidden
    at _getCrumb (###/node_modules/yahoo-finance2/dist/esm/src/lib/getCrumb.js:204:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.yahooFinanceFetch [as _fetch] (file:###/yahoo-finance2/dist/esm/src/lib/yahooFinanceFetch.js:53:23)
    at async Object.moduleExec [as _moduleExec] (file:###/node_modules/yahoo-finance2/dist/esm/src/lib/moduleExec.js:52:18)
    at async getStockPrice (file:###/yahoo/api.js:6:19)
glaucoheitor commented 4 days ago

Check to see if you're able to manually go to https://query1.finance.yahoo.com/v1/test/getcrumb on the browser.