401 Client Error: Unauthorized for url

Hi, I am getting this error when trying to run my contract over brownie:

Brownie v1.19.0 - Python development framework for Ethereum

BrownieSimpleStorageProject is the active project.

Running 'scripts/deploy.py::main'...
  File "brownie/_cli/run.py", line 51, in main
    return_value, frame = run(
  File "brownie/project/scripts.py", line 110, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File "./scripts/deploy.py", line 23, in main
    deploy_simple_storage()
  File "./scripts/deploy.py", line 6, in deploy_simple_storage
    simple_storage = SimpleStorage.deploy({"from": account})
  File "brownie/network/contract.py", line 549, in __call__
    return tx["from"].deploy(
  File "brownie/network/account.py", line 510, in deploy
    receipt, exc = self._make_transaction(
  File "brownie/network/account.py", line 720, in _make_transaction
    gas_price, gas_strategy, gas_iter = self._gas_price(gas_price)
  File "brownie/network/account.py", line 456, in _gas_price
    return web3.eth.generate_gas_price(), None, None
  File "web3/eth.py", line 986, in generate_gas_price
    return self._generate_gas_price(transaction_params)
  File "web3/eth.py", line 213, in _generate_gas_price
    return self.gasPriceStrategy(self.web3, transaction_params)
  File "web3/gas_strategies/rpc.py", line 20, in rpc_gas_price_strategy
    return web3.manager.request_blocking(RPC.eth_gasPrice, [])
  File "web3/manager.py", line 197, in request_blocking
    response = self._make_request(method, params)
  File "web3/manager.py", line 150, in _make_request
    return request_func(method, params)
  File "web3/middleware/formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "web3/middleware/gas_price_strategy.py", line 90, in middleware
    return make_request(method, params)
  File "web3/middleware/formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "web3/middleware/attrdict.py", line 33, in middleware
    response = make_request(method, params)
  File "web3/middleware/formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "web3/middleware/formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "web3/middleware/formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "web3/middleware/buffered_gas_estimate.py", line 40, in middleware
    return make_request(method, params)
  File "web3/middleware/exception_retry_request.py", line 105, in middleware
    return make_request(method, params)
  File "web3/providers/rpc.py", line 88, in make_request
    raw_response = make_post_request(
  File "web3/_utils/request.py", line 103, in make_post_request
    response.raise_for_status()
  File "requests/models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 401 Client Error: Unauthorized for url: https://rinkeby.infura.io/v3/{my API key}

This is the command I am running :

brownie run scripts/deploy.py --network rinkeby

I set my .env file like this:

export PRIVATE_KEY={private key}
export WEB3_INFURA_PROJECT_ID={my API key}

I tried to:
-Delete my account and create a new one
-Delete and create new projects over and over
-Change to another testnet like Ropsten

My rinkeby wallet has exactly 0.2357RinkebyETH.

Please, if someone could help me I will apreciate it.

Hi there Pablo and welcome to our community!

On a first look, it might be that your .env file is set up incorrectly. Here is how mine is configured (the project ID and Private Key are dummy data that do not work, these should never be shared with anyone else):

Then, to access the private key in my python code I use ‘os’, like this:

import os

private_key = os.getenv('SIGNER_PRIVATE_KEY')

The problem is solved!!!

In the infura site:
Step 1. Create IPFS project with card info
Step 2. replace the following code with your old code

const projectId = ‘2E8Kps3Xij…’; //(Step 3. Place the project id from your infura project)
const projectSecret = ‘b222afc94…’; //(Step 4. Place the project_secrect from your infura project)

const auth = 'Basic ’ + Buffer.from(projectId + ‘:’ + projectSecret).toString(‘base64’);

const ipfsClient = require(‘ipfs-http-client’)
const ipfs = ipfsClient.create({
host: “ipfs.infura.io”,
port: 5001,
protocol: “https”,
apiPath: “/api/v0”,
headers: {
authorization: auth,
},
});

Step 5. Save and npm start