def sellOrder(self, amount, price):
url=self.binance_url+"/fapi/v1/order" #api 주소
servertime = requests.get("https://api.binance.com/api/v1/time") #servertime을 받아옴(timestamp 파라미터로 같은 시간에 보낸건지 확인하는거떔에)
servertimeobject = json.loads(servertime.text)
servertimeint = servertimeobject['serverTime']
#request body로 보낼 것들 딕셔너리
request_body={
'symbol':'BTCUSDT',
'side': 'SELL',
'positionSide':'SHORT',
'orderType':'LIMIT',
'timeInForce':'GTC',
'quantity':float(amount),
'price': float(price),
'recvWindow':6000,
'timestamp':servertimeint
}
request_body=self.encoding(request_body) #딕셔너리 내용을 api키로 암호화(딕셔너리에 signature라고 전체 딕셔너리를 암호화한 키-밸류 하나를 넣음)
headers={
'X-MBX-APIKEY':self.API_KEY, #헤더로 넣을 내용들
'Content-Type':'x-www-form-urlencoded'
}
req=requests.post(url, data=request_body, headers=headers) #post로 url에 데이터를 바디로 보내고 헤더도 보냄
response=json.loads(req.text)
return response
이렇게 했는데 이 응답이 {'code': -1102, 'msg': "Mandatory parameter 'timestamp' was not sent, was empty/null, or malformed."}
이렇게 뜨는데 print(request_body)를 인코딩 행 뒤에 넣어도 timestamp키랑 값은 그대로 존재하고있는데, 자꾸 '필수 인자인 timestamp가 병신입니다.'라고
응답이 오는데, 내가 body로 안보내고 있는거야??
For POST, PUT, and DELETE endpoints, the parameters may be sent as a query string or in the request body with content type application/x-www-form-urlencoded. You may mix parameters between both the query string and request body if you wish to do so.
Parameters may be sent in any order.
If a parameter sent in both the query string and request body, the query string parameter will be used.
이게 거래서 General API Information인데, Post는 인자를 쿼리열이나, content type이 www뭐시기인 request body로 보내면 되고,
파라미터는 순서 좆대로 해도 된다는 내용인데 왜 내가 body로 보낸 request_body에서 timestamp를 지 맘대로 빼냐 이거지... 혹시 도움 줄 수 있워??
req=requests.post(url, data=request_body, headers=headers) #post로 url에 데이터를 바디로 보내고 헤더도 보냄 data=request_body
req=requests.post(url, data=request_body, headers=headers) #post로 url에 데이터를 바디로 보내고 헤더도 보냄 json=request_body 이렇게 보내봐
오우 자주 못해서 이제야 봤어용 근데 json으로 보냈는데도 똑같이 오류가 뜨네요 ;ㅅ; print(request_body)를 해보면 request_body가 이렇게 나오는데, {'symbol': 'BTCUSDT', 'side': 'BUY', 'positionSide': 'LONG', 'orderType': 'LIMIT', 'timeInForce': 'GTC', 'quantity': 0.001, 'price': 10000.0, 'recvWindow': 6000, 'timestamp': 1601256837203, 'signature': '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'} 여기 분명히 인자로 timestamp가 들어있는데도 왜 계속 오류가 뜨는지 모르겠네요 ㅠ
아니 이거 나 봤는데 이렇게 하니까 되네?? 아래 헤더를 application/x-www-form-urlencoded로 바꾸고 그냥 data= 이걸 지우고 그냥 순서대로 위치전달했거든(위치전달이라고 부르는게 맞는지 모르겠다 그냥 함수 파라미터명=변수 이렇게 안하고 변수, 변수 이렇게 써넣는거)?? header만 지정해서 전달하고?? 그니까 갑자기 'type'인자가 빠져있다고 오류가 바뀌길래 type를 request_body에다가 넣으니까 정상적으로 작동되더라?? 신기하네;;; 내가 헤더를 잘못 썼었나봐 ;ㅅ;