Forums

403 Forbidden for finnhub

Hello. I am trying to setup a backend using flask which takes in finnhub financial data and deploy it so that (in the future) my front-end can use it. Currently, I am getting this error:

{ "error": "HTTPSConnectionPool(host='api.finnhub.io', port=443): Max retries exceeded with url: /api/v1//stock/financials-reported?token=________&symbol=AAPL&freq=quarterly (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 403 Forbidden')))" }

As far as I understand, finnhub.io is among the allowlisted sites, so I should be able to get data from its API. Locally, my code runs. So I expected the snippet I am trying to use ought to run as well. From the link below, I think I am supposed to use a proxy, but I am a bit unclear about how to implement it.

https://help.pythonanywhere.com/pages/403ForbiddenError/

from flask import Flask
import finnhub
import os
from dotenv import load_dotenv

# Setup finnhub client
project_folder = os.path.expanduser('~/backend')
load_dotenv(os.path.join(project_folder,'.env'))
FINNHUB_API_KEY = os.getenv("FINNHUB_API_KEY")
finnhub_client = finnhub.Client(api_key=f'{FINNHUB_API_KEY}')

app = Flask(__name__)

if __name__ == '__main__':
    app.run()

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

@app.route('/stocks_temp')
def stocks_temp():

    try:
        print('yes!')
        earn1 = finnhub_client.financials_reported(symbol='AAPL', freq='quarterly')['data']

        return str(earn1)
    except Exception as e:
        return {'error': str(e)}, 500

finnhub api docs: https://finnhub.io/docs/api

You'll likely have to request for the api.finnhub.io domain to be whitelisted

Thanks for letting me know. I will try to do that.

Here's a link to the page with the details for doing that.

Thanks for the help. My issue is resolved after api.finnhub.io has been whitelisted.

Lovely!