sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.14k stars 2.23k forks source link

TESTNET: binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID. #1155

Open zenthara opened 2 years ago

zenthara commented 2 years ago

Describe the bug I tried to connect to the binance testnet and to get the account status, but got the following error:

File "/home/h/anaconda3/envs/pairTrading/lib/python3.9/site-packages/binance/client.py", line 2065, in get_account_status return self._request_margin_api('get', 'account/status', True, data=params) File "/home/h/anaconda3/envs/pairTrading/lib/python3.9/site-packages/binance/client.py", line 365, in _request_margin_api return self._request(method, uri, signed, **kwargs) File "/home/h/anaconda3/envs/pairTrading/lib/python3.9/site-packages/binance/client.py", line 316, in _request return self._handle_response(self.response) File "/home/h/anaconda3/envs/pairTrading/lib/python3.9/site-packages/binance/client.py", line 325, in _handle_response raise BinanceAPIException(response, response.status_code, response.text) binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

I found out, that when I call client.get_account_status(), the client connects to the real binance api and not to the testnet api because it doesn't switch from the real url to the testnet url:

   def _create_margin_api_uri(self, path: str, version: str = MARGIN_API_VERSION) -> str:
        return self.MARGIN_API_URL + '/' + version + '/' + path

To compare with other requests that works:

    def _create_api_uri(self, path: str, signed: bool = True, version: str = PUBLIC_API_VERSION) -> str:
        url = self.API_URL
        if self.testnet:
            url = self.API_TESTNET_URL
        v = self.PRIVATE_API_VERSION if signed else version
        return url + '/' + v + '/' + path

To Reproduce client = Client(config.api_key, config.secret_key, testnet=True) client.get_account_status()

Expected behavior Response from the Testnet.

Environment (please complete the following information):

Logs or Additional context Add any other context about the problem here.

Pragmatich commented 2 years ago

I got the same error, when I tried to activate the futures trading on testnet with test api key:

binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

client = Client(api_key, api_secret, testnet=True)

client.enable_subaccount_futures()

Maybe with test api key not allowed futures operations?

OlMi1 commented 2 years ago

Maybe with test api key not allowed futures operations?

What country are you from? I have the feeling that it is because in my case I am German and it is very unclear what services they operate here or not. Same borders also apply to Italy and some other EU countries

Pragmatich commented 2 years ago

What country are you from? I have the feeling that it is because in my case I am German and it is very unclear what services they operate here or not. Same borders also apply to Italy and some other EU countries

I'm from Hungary. So maybe that's the case.

claude-eric-steiner commented 2 years ago

running in the same issue. NOT COOL. If you can't rely that if you set testnet=True that all traffic is really getting to the testnet and not the real account. Thanks god there are different api_key/secret so it can not corrupt a real account. Anyway really not cool... And is there any fix? This issue is open since March 2, and still not fixed.

erik404 commented 2 years ago

I made a fix and PR for the get_futures_account because there is no check in that function to respect the test net argument. It failed on the requirements file, which is a different issue :-/

kinda bummed out, I really like the library but I feel like it's dead because there is no communication on potentially dangerous issues when they are reported.

claude-eric-steiner commented 2 years ago

thanks erik404, seems I need to look for an alternative then...

erik404 commented 2 years ago

I'll make a new PR and also fix the margin_account, I wasn't aware that the test net argument was not respected there as well and see what's up with the failing build, give me a couple of hours 🤞

erik404 commented 2 years ago

worst case we can fork, implement fixes, put it up pip until we get some sort of response from the repo owner

claude-eric-steiner commented 2 years ago

cool thanks!

erik404 commented 2 years ago

PR request: https://github.com/sammchardy/python-binance/pull/1205

edit:

Travis CI build fails only on Python 3.7 build on pytest-pep8... other builds are fine. Maybe @sammchardy can have a look?

claude-eric-steiner commented 2 years ago

Thank you erik404, I will go through your changes and check them out

claude-eric-steiner commented 2 years ago

I'm still having the same issue: Traceback (most recent call last): File "testOrder2.py", line 33, in <module> status = client.get_account_status() File "c:\python38\lib\site-packages\binance\client.py", line 2076, in get_account_status return self._request_margin_api('get', 'account/status', True, data=params) File "c:\python38\lib\site-packages\binance\client.py", line 364, in _request_margin_api return self._request(method, uri, signed, **kwargs) File "c:\python38\lib\site-packages\binance\client.py", line 315, in _request return self._handle_response(self.response) File "c:\python38\lib\site-packages\binance\client.py", line 324, in _handle_response raise BinanceAPIException(response, response.status_code, response.text) binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

seems to fail here then: def _create_margin_api_uri(self, path: str, version: str = MARGIN_API_VERSION) -> str: return self.MARGIN_API_URL + '/' + version + '/' + path I see a check in _create_api_uri for the testnet, but it's missing in _create_margin_api_uri. If I want to follow the same approach, I'm missing an self.MARGIN_API_TESTNET_URL

:/

erik404 commented 2 years ago

I see. Thats in the REST API client. The fix I proposed is for the socket connections. I have found a possible solution. Can you confirm that the margin_api_url is the same as the API_TESTNET_URL (https://testnet.binance.vision/api)?

hamishkr commented 2 years ago

on the Spot Testnet I don't think /sapi/* endpoints are available including Wallet Endpoints

Yooo31 commented 1 year ago

Hello I have the problem, I have :

client = Client(api_key=YOUR_API_KEY, api_secret=YOUR_API_SECRET asset_details = client.get_margin_asset(asset='BNB')

Terminal return : binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

But I didn't find the resolution to my problem, have you got an idea ?

sirfrank commented 1 year ago

i believe you need API key specially for Testnet. (as far as i remember, i never used)

Yooo31 @.***> ezt írta (időpont: 2023. jan. 13., P, 12:37):

Hello I have the problem, I have :

client = Client(api_key=YOUR_API_KEY, api_secret=YOUR_API_SECRET asset_details = client.get_margin_asset(asset='BNB')

Terminal return : binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

But I didn't find the resolution to my problem, have you got an idea ?

— Reply to this email directly, view it on GitHub https://github.com/sammchardy/python-binance/issues/1155#issuecomment-1381733151, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVKGM4MTQ4TUPKDG72MGJDWSE47PANCNFSM5PX5BXUQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Yooo31 commented 1 year ago

Yes I have link my Github account with testNet binance but I have this error

je crois que vous avez besoin d'une clé API spécialement pour Testnet. ( pour autant que je m'en souvienne, je jamais utilisé ) Yooo31 @ . > ezt írta ( időpont: 2023. jan. 13., P, 12:37 ): … Hello I have the problem, I have : client = Client(api_key=YOUR_API_KEY, api_secret=YOUR_API_SECRET asset_details = client.get_margin_asset(asset='BNB') Terminal return : binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID. But I didn't find the resolution to my problem, have you got an idea ? — Reply to this email directly, view it on GitHub <#1155 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVKGM4MTQ4TUPKDG72MGJDWSE47PANCNFSM5PX5BXUQ . You are receiving this because you are subscribed to this thread.Message ID: @.>

sirfrank commented 1 year ago

try the API key with other function. ALSO check, if API key is valid on Bonance API management, because new rule at Binance : certain API KEYS deleted if no IP restriction on them.

Yooo31 @.***> ezt írta (időpont: 2023. jan. 13., P, 16:19):

je crois que vous avez besoin d'une clé API spécialement pour Testnet. ( pour autant que je m'en souvienne, je jamais utilisé ) Yooo31 *** @

*. ** > ezt írta ( időpont: 2023. jan. 13., P, 12:37 ): … <#m-79705632453120414> Hello I have the problem, I have : client = Client(api_key=YOUR_API_KEY, api_secret=YOUR_API_SECRET asset_details = client.get_margin_asset(asset='BNB') Terminal return : binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID. But I didn't find the resolution to my problem, have you got an idea ? — Reply to this email directly, view it on GitHub <#1155 (comment) https://github.com/sammchardy/python-binance/issues/1155#issuecomment-1381733151>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVKGM4MTQ4TUPKDG72MGJDWSE47PANCNFSM5PX5BXUQ https://github.com/notifications/unsubscribe-auth/ABVKGM4MTQ4TUPKDG72MGJDWSE47PANCNFSM5PX5BXUQ . You are receiving this because you are subscribed to this thread.Message ID: @.>

Yes I have link my Github account with testNet binance but I have this error

— Reply to this email directly, view it on GitHub https://github.com/sammchardy/python-binance/issues/1155#issuecomment-1382007874, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVKGMZDXZVVHWH4JWIX3J3WSFXAZANCNFSM5PX5BXUQ . You are receiving this because you commented.Message ID: @.***>

deltawi commented 1 year ago

Any updates on this ? I am trying to do a simple operation on testnet after generating the key/secret from here https://testnet.binance.vision/ and the code:

# Initialize Binance client
client = Client(api_key=api_key,
                api_secret=api_secret,
                testnet=True)

# Perpetual contract symbol
symbol = "SHIBUSDT_PERP"

client.get_margin_price_index(symbol=symbol)["price"]

I get binance.exceptions.BinanceAPIException: APIError(code=-2008): Invalid Api-Key ID.

MaksLuk commented 1 year ago

I can place order (client.futures_create_order(...)) but can't use client.get_all_orders, client.cancel_order, client.get_order... Error: binance.exceptions.BinanceAPIException: APIError(code=-2015): Invalid API-key, IP, or permissions for action.

esketids commented 1 year ago

bro

chan1919 commented 1 year ago

@MaksLuk boy,how do u solve this problem?

KALIU-SOUZA commented 10 months ago

Estou com o mesmo problema e não consigo achar solução em nenhum fórum.

amanpreetsingh-BE commented 8 months ago

Hello Don't forget to add the parameter tld="vision" for the Testnet, by default it is set to ".com" but the Testnet is on ".vision" tld ... so you have an instance of the Binance Client as : binanceclient = Client(api_key=binance_api_key, api_secret=binance_secret_key, testnet=True, tld="vision") Where binance_api_key and binance_secret_key are your keys generated on https://testnet.binance.vision