简体   繁体   中英

Error: API_KEY not set in VS Code Desktop

I am experimenting with the IEX API. I have created a website to query stock info via a symbol (eg NFLX or AAPL) in an IDE provided via the edX Harvard CS50x course and am now trying to set up VS Code desktop to be independent of their provided IDE.

I am struggling to set my API_KEY in VS Code, desktop version. I downloaded VS Code (desktop version +python, nodejs, flask, etc. etc.), cloned my repository from Github and then tried to run the website to query stock info (which works perfectly fine in the IDE after setting my api-key).

The code for the key looks as follows:

# Contact API
try:
    api_key = os.environ.get("API_KEY")
    url = f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}"
    response = requests.get(url)
    response.raise_for_status()

except requests.RequestException:
    return None

I get the following error: API_KEY not set in VS Code Desktop

I am working on windows and have managed to set the flask app with this syntax: set FLASK_APP=app.py

I set the API_KEY accordingly ( set API_KEY=xxx ), but when running flask, I get the error above. I use this command to run flask: python3 -m flask run

I tried to set the API_KEY within the app.py file, just to see if it would work (despite security issues), but no success: os.environ["API_KEY"] = "xxx"

Any experiences with this in windows? I found this stackoverflow post, but that guy didn't get an answer either: How do I set an API_KEY in Windows terminal?

I managed to make it work: My FLASK_APP app.py file was checking if the API_KEY was set like this:

# Make sure API key is set

if not os.environ.get("API_KEY"):

        raise RuntimeError("API_KEY not set")

I do not know how to set the api key with the correct syntax, so I commented this part out and inserted the api key as a string in my code instead: api_key = 'xxx'

Apparently, this is now a security issue, since I hard-coded my api key, but at least it runs now on windows in the VS code desktop version. I also tried to hide the key away in a .env file which works ( How to set and get environment variables in python ) but still no solution for setting it from the command line.

Make sure you read the specification in full! https://cs50.harvard.edu/x/2022/psets/9/finance/ The problem specifically states how to set up an API Key. I will paste it here for convenience:

Configuring

Before getting started on this assignment, we'll need to register for an API key in order to be able to query IEX's data. To do so, follow these steps:

Visit iexcloud.io/cloud-login#/register/.

Select the “Individual” account type, then enter your name, email address, and a password, and click “Create account”.

Once registered, scroll down to “Get started for free” and click “Select Start plan” to choose the free plan.

Once you've confirmed your account via a confirmation email, visit https://iexcloud.io/console/tokens .

Copy the key that appears under the Token column (it should begin with pk_).

In your terminal window, execute:

$ export API_KEY=value

where value is that (pasted) value, without any space immediately before or after the =. You also may wish to paste that value in a text document somewhere, in case you need it again later.

I don't know if you are still working on this problem, but I'll still post this just in case any future users need help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM