python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

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

Open zenthara opened this issue 3 years ago • 24 comments

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):

  • Python version: 3.9
  • Virtual Env: conda
  • OS: RHEL 8.4
  • python-binance version: 1.0.15

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

zenthara avatar Mar 02 '22 16:03 zenthara

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?

Pragmatich avatar Mar 14 '22 14:03 Pragmatich

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

OlMi1 avatar Apr 07 '22 18:04 OlMi1

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.

Pragmatich avatar Apr 08 '22 09:04 Pragmatich

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.

claude-eric-steiner avatar Jun 22 '22 15:06 claude-eric-steiner

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.

erik404 avatar Jun 22 '22 15:06 erik404

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

claude-eric-steiner avatar Jun 22 '22 15:06 claude-eric-steiner

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 avatar Jun 22 '22 15:06 erik404

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

erik404 avatar Jun 22 '22 15:06 erik404

cool thanks!

claude-eric-steiner avatar Jun 22 '22 15:06 claude-eric-steiner

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?

erik404 avatar Jun 22 '22 16:06 erik404

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

claude-eric-steiner avatar Jun 22 '22 18:06 claude-eric-steiner

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

:/

claude-eric-steiner avatar Jun 22 '22 18:06 claude-eric-steiner

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)?

erik404 avatar Jun 22 '22 20:06 erik404

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

hamishkr avatar Jul 04 '22 04:07 hamishkr

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 ?

Yooo31 avatar Jan 13 '23 11:01 Yooo31

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: @.***>

sirfrank avatar Jan 13 '23 13:01 sirfrank

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: @.>

Yooo31 avatar Jan 13 '23 15:01 Yooo31

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: @.***>

sirfrank avatar Jan 13 '23 15:01 sirfrank

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.

deltawi avatar Apr 06 '23 21:04 deltawi

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.

MaksLuk avatar Apr 15 '23 15:04 MaksLuk

bro

esketids avatar May 09 '23 07:05 esketids

@MaksLuk boy,how do u solve this problem?

chan1919 avatar Jun 12 '23 09:06 chan1919

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

KALIU-SOUZA avatar Jan 16 '24 17:01 KALIU-SOUZA

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

amanpreetsingh-BE avatar Apr 01 '24 22:04 amanpreetsingh-BE