Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zenthara opened this issue Mar 2, 2022 · 24 comments

Comments

@zenthara
Copy link

zenthara commented Mar 2, 2022

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.

@Pragmatich
Copy link

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
Copy link

OlMi1 commented Apr 7, 2022

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
Copy link

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
Copy link

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
Copy link
Contributor

erik404 commented Jun 22, 2022

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
Copy link

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

@erik404
Copy link
Contributor

erik404 commented Jun 22, 2022

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
Copy link
Contributor

erik404 commented Jun 22, 2022

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
Copy link

cool thanks!

@erik404
Copy link
Contributor

erik404 commented Jun 22, 2022

PR request: #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
Copy link

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

@claude-eric-steiner
Copy link

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
Copy link
Contributor

erik404 commented Jun 22, 2022

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
Copy link

hamishkr commented Jul 4, 2022

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

@Yooo31
Copy link

Yooo31 commented Jan 13, 2023

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
Copy link

sirfrank commented Jan 13, 2023 via email

@Yooo31
Copy link

Yooo31 commented Jan 13, 2023

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
Copy link

sirfrank commented Jan 13, 2023 via email

@deltawi
Copy link

deltawi commented Apr 6, 2023

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
Copy link

MaksLuk commented Apr 15, 2023

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
Copy link

esketids commented May 9, 2023

bro

@chan1919
Copy link

@MaksLuk boy,how do u solve this problem?

@KALIU-SOUZA
Copy link

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

@amanpreetsingh-BE
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

15 participants