Hi! I try to use example SIGNED Endpoint from Binance API Documentation for signed request Get sapi/v1/capital/config/getall in Python Original example uses .pem file which has a secret key. I don’t know how to make pem file and use just → secret_key=‘aTysuid5kjsd…’ instead of access to pem file. The result (?) is an error ““AttributeError: ‘str’ object has no attribute ‘sign’””" My question is it’s because of using just string instead of pem file? If so how to make such a pem file? Can I download it from my Binance Personal Area? Here is my full Python code:
import base64
import requests
import time
from cryptography.hazmat.primitives.serialization import load_pem_private_key
payload = ‘&’.join([f’{param}={value}’ for param, value in params.items()])
signature = base64.b64encode(private_key.sign(payload.encode(‘ASCII’)))
params[‘signature’] = signature
You can generate a key pair using the Asymmetric Key Generator program. You would then save the private key and keep that to yourself and copy the public key to paste in the new API creation area on binance.com. In your code, you would load the contents of the private key .pem file as so:
with open(PRIVATE_KEY_PATH, 'rb') as f:
private_key = load_pem_private_key(data=f.read(),
password=None)
Oh so you are using the standard API key/secret pair then? Plaintext API key + plaintext Secret key generated directly on the Binance.com website? If so, then it’s a different process. The process outlined above is only if you’re using ED25519/RSA Keys where you generate the keys locally, only share your public key with Binance and in return are provided with an API key. You should be following this example if that’s the case: https://github.com/binance/binance-signature-examples/blob/master/python/hmac_sha256_signature.py
#######################################################
with open(PRIVATE_KEY_PATH, ‘rb’) as f:
private_key = load_pem_private_key(data=f.read(),
password=None)
########################################################
returns
###########################################################
FileNotFoundError Traceback (most recent call last)
Cell In[164], line 1
----> 1 with open(PRIVATE_KEY_PATH, ‘rb’) as f:
2 private_key = load_pem_private_key(data=f.read(),
3 password=None)
File ~\anaconda3\Lib\site-packages\IPython\core\interactiveshell.py:284, in _modified_open(file, *args, **kwargs)
277 if file in {0, 1, 2}:
278 raise ValueError(
279 f"IPython won’t let you open fd={file} by default "
280 "as it is likely to crash IPython. If you know what you are doing, "
281 “you can use builtins’ open.”
282 )
→ 284 return io_open(file, *args, **kwargs)
FileNotFoundError: [Errno 2] No such file or directory: ‘./private_key.pem’