bad_signature_gneration

Trying to start using binance API (zero python experience, some R, some java). First tried R - binance.R, binancer. Moved to python. Copied a python code from gemini. This is what gemini suggested to compute signature:

def get_assets_info():
“”"
Retrieves account information from the Binance API.
“”"

url = “https://api.binance.com/api/v3/account
api_key = “your_api_key” # Replace with your actual API key
api_secret = “your_api_secret” # Replace with your actual API secret

headers = {
“X-MBX-APIKEY”: api_key
}

timestamp = int(time.time() * 1000) # Get timestamp in milliseconds
params = {“timestamp”: timestamp}

URL encode the parameters

encoded_params = urlencode(params)

Combine timestamp and encoded params for signature message

message = f"{timestamp}{encoded_params}"

Sign the message using HMAC-SHA256 with your API secret

signature = hmac.new(api_secret.encode(), message.encode(), hashlib.sha256).hexdigest()

params[“signature”] = signature

print(“Encoded params:”, encoded_params)
print(“Signature:”, signature)

response = requests.get(url, headers=headers, params=params)

if response.status_code == 200:
return response.json()
else:
print(“Error:”, response.text)

print("Response headers:", response.headers)
return None 

  1. My python says, among other comments: Error: {“code”:-2014,“msg”:“API-key format invalid.”}

  2. After I enter the python-computed signature value at Postman, it says: “code”: -1102,
    “msg”: “Mandatory parameter ‘signature’ was not sent, was empty/null, or malformed.”

Would appreciate comments. Another question - apart from python what are other popular languages to start using binance API. Thanks in advance.

Hey,
what happens if you remove {timestamp} from the line message = f"{timestamp}{encoded_params}"?
Otherwise, I had suggest checking out the different connectors provided by Binance: Binance · GitHub

I can generate Client, get binance prices, But client.get_account(signature=True) does not work, says - Error retrieving account information: APIError(code=-1022): Signature for this request is not valid.

from binance.client import Client
from binance.exceptions import BinanceAPIException
import os

api_key = os.getenv(“api_key”)
api_secret = os.getenv(“api_secret”)

client = Client(api_key, api_secret)

print(client)

ticker = client.get_symbol_ticker(symbol=“BTCUSDT”)
print(f"Current price of Bitcoin: {ticker[‘price’]}")

try:

Get account information (explicitly setting signature for clarity)

account_info = client.get_account(signature=True)

Print account information

print(account_info)
except BinanceAPIException as e:
print(f"Error retrieving account information: {e}")

Have a look at this post, it may help you: FAQ: Signature for this request is not valid.