1022 Signature for this request is not valid. error for the 2nd+ time

Hi, I’m trying to send balance requests with my python code. However, I found that the code only works for the first time. When I send the request second time, I’ll get the 1022 Signature for this request is not valid error. Is there any idea that cause the error?

My code:

import requests
import json
import hashlib, hmac
from urllib.parse import urlencode


class BinanceFutures():
	def __init__(self, testing = True):
		self._apiSecret = "I'm sure it is correct"
		self._defaultHeaders = {"X-MBX-APIKEY": "I'm sure it is correct"}
		
		self.apiBaseUrl = "https://testnet.binancefuture.com" if testing else "https://fapi.binance.com"
		
	def _getServerTime(self):
		serverTime = requests.get("https://api.binance.com/api/v1/time")
		serverTimeObject = json.loads(serverTime.text)
		return serverTimeObject['serverTime']
		
	def _signQuery(self, query):
		return hmac.new(self._apiSecret.encode('utf-8'), query.encode('utf-8'), hashlib.sha256).hexdigest()
		
		
	### GET REQUESTS
	def _simpleGetRequest(self, urlPath, params = {}):
		params['timestamp'] = self._getServerTime()
		params['signature'] = self._signQuery( urlencode(params) )
		
		# I'd tried both of these, same error occurred
		# res = requests.get(f'{self.apiBaseUrl}{urlPath}', params = params, headers = self._defaultHeaders)
		res = requests.get(f'{self.apiBaseUrl}{urlPath}?{urlencode(params)}', headers = self._defaultHeaders)
		return res.status_code, res.text
		
		
	# Get Account Info
	def account(self):
		return self._simpleGetRequest('/fapi/v2/account')
		
		
	# Get Current Balance
	def balance(self):
		return self._simpleGetRequest('/fapi/v2/balance')

Additional:
I’ve confirmed these steps:

I don’t know python, but at a glance it looks like this line is the issue -

params['signature'] = self._signQuery( urlencode(params) )

Second time params probably contains the ‘signature’ field already. Try setting ‘signature’ directly at the url. eg. -

{self.apiBaseUrl}{urlPath}?{urlencode(params)}&signature=...

Thank you for answering, I tried to pring out the params at the start of the _simpleGetRequest and find that it do contains the previous signature, which apparently causes the issue. Thank you!