简体   繁体   中英

api key reading config file - binance trading bot python: SyntaxError: (unicode error)

I've just started today to code: so please be kind! :D

After a long we to install programs and libraries in coding my fist "trading bot" I'm trying to connect to my test-api in Binance but I have an issue.

Ive already done:

  1. uninstall and install again pip python-binance
  2. install conda twisted
  3. install pip update same packages

Any suggestions?

Many thanks

--- edit photo of errors

ive tried all 3 types of suggestion

now pythons tell me there there is no module named 'binance' how is possible?

- relevant code

# Importing libraries 
from binance.client import Client
import configparser

# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open('<C:\\Users\\ssida\\OneDrive\\Documenti\\GitHub\\AI7XF205SS\\secret.cfg>'))
test_api_key = config.get('BINANCE', 'TEST_API_KEY')
test_secret_key = config.get('BINANCE', 'TEST_SECRET_KEY')

client = Client(test_api_key, test_secret_key)

client.API_URL = 'https://testnet.binance.vision/api'  # To change endpoint URL for test account  

info = client.get_account()  # Getting account info

print(info)

edit 2x --- i think che path now works, but now i have a new issue... PI Error (code+-2014).. gonna search what is that..

seems that API key format is invalid:(

[Running] python -u "c:\Users\ssida\OneDrive\Documenti\GitHub\AI7XF205SS\getting_account_info.py"
Traceback (most recent call last):
  File "c:\Users\ssida\OneDrive\Documenti\GitHub\AI7XF205SS\getting_account_info.py", line 15, in <module>
    info = client.get_account()  # Getting account info
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 1822, in get_account
    return self._get('account', True, data=params)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 292, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 242, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 237, in _request
    return self._handle_response()
  File "C:\Users\ssida\anaconda3\lib\site-packages\binance\client.py", line 285, in _handle_response
    raise BinanceAPIException(self.response)
binance.exceptions.BinanceAPIException: APIError(code=-2014): API-key format invalid.

[Done] exited with code=1 in 1.944 seconds

The problem is the backslash is a special character (ex \n marks a new line)

You can fix this by either adding r before the path

r'C:\Users\...'

or change the backslash with double ones

'C:\\Users\\...'

or replace with '/'

'C:/Users/...'

EDIT

Remove the '<>' from the start and end of the path because open takes it as it is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM