Signed request problem (Python)

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

api_key = ‘RNy…’
private_key = ‘p74W…’

params = {}

Timestamp the request

timestamp = int(time.time() * 1000) # UNIX timestamp in milliseconds
params[‘timestamp’] = timestamp

Sign the request

payload = ‘&’.join([f’{param}={value}’ for param, value in params.items()])
signature = base64.b64encode(private_key.sign(payload.encode(‘ASCII’)))
params[‘signature’] = signature

Send the request

headers = {
‘X-MBX-APIKEY’: API_KEY,
}
response = requests.get(
https://api.binance.com/sapi/v1/capital/config/getall’,
headers=headers,
data=params,
)
resp=response.json()

Hi there,

You can take a look at the signature-examples Github repo and use the example code as a reference point.

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)

Hi! Thank you for the reply. I made my test exactly by https://github.com/binance/binance-signature-examples/blob/master/python/ed25519_signature.py but the problem may be 1) I don’t know how to create pem file 2) My keys are Hmac. Are they suitable for this example of code ? ( with base64 module)

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

Hello,

I’m using ED25519/RSA Keys and the command:

#######################################################
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’

###############################################################

Do I have to generate the .pem file?

When I generated the ED25519 keys, I saved it as .txt but it doesn’t work. Then I change the extension.

Thank you,

Regards