Ethereum: Python Binance API binance.exceptions.BinanceAPIException: APIError(code=-2015): Invalid API-key, IP, or permissions for action

Here is the modified version of the code with some improvements in the field of safety and error:

`Python

Import os

With Binance Import Client

Ethereum: Python Binance API binance.exceptions.BinanceAPIException: APIError(code=-2015): Invalid API-key, IP, or permissions for action

Use the latest version by installing "Upgrade Pip installation installation" in the terminal.

Api_key = "Your_api_Key"

Replace the actual bees key.

Api_secret = "Your_api_secret"

Replace the actual secret of the API.

Testnet = False

TUUE SET FOR THE TEST OR FALSE NETWORK FOR MAINNET.

Def Get_client (Api_key, Api_Secret):

"" ""

Start an API Binance client with the API key and the secret.

Args:

Api_key (Str): the Binance Api key.

Api_secret (p.): Your secret binance.

Return:

Customer: Customer Binance API started.

"" ""

If not os.environ.get ("binance_api_key", "") .strip ():

Valowerror evaluation ("Lack of the Binance Api key in the environmental variable.")

Elif Not Os.environ.get ("Binance_api_secret", "") .strip ():

More Valiteerror ("Your secret binance secret is missing in the environmental variable.")

Customer (

aPi_key = Api_Key,

Api_secret = Api_Secret,

Testnet = testnetnet

)

Def Main ():

"" ""

Get information on the account and perform the function.

"" ""

Client = Get_client (Api_Key, Api_Secret)

To try:

Info = Client.get_acCount ()

PRINT (F "Account status: {info}")

Add the desired action here ...

For example, you can use a customer to place an order or download market data

Order = client.place_ormer (symbol = "btcusdt", page = "buy")

Print ("Order ID:", Orders)

Except the exception as E:

PRINT (F "Error: {e}")

If __name__ == "__Main__":

principal()

`

Here is the list of improvements I introduced:

  • The error service was added : the original code did not have a test block, which means that if the error occurs during the execution, the program would have been immediately promoted. Now we add a test block to capture all the exceptions that can occur.

  • Configuration of the variable environment : instead of a permanent bees key and a mystery in the code, we added control to make sure that they are set as environmental variables. In this way, it is possible to easily pass between the interfaces API Mainnet and Testnet, correctly setting the environmental variable.

  • Docstings

    added: we have attached documents for each function to provide information on what functions, their parameters and any exceptions that can be raised.

  • The improved readability of the code : I used significant variable names and added comments to explain the purpose of each section of the code.

5 Now we have moved on to the API Mainnet interface if the “testnet” flag is set to “False”.

Leave a Comment

Your email address will not be published. Required fields are marked *