whitebit-exchange / api-quickstart

Examples of how to access WhiteBIT API in Python, Node.js, PHP, Go, Java and more

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot use Auth features with Golang

kaankoken opened this issue · comments

Hi,
I have been trying to use the authenticated user feature for my golang app, and it does not work. I used a very similar code that shared on Github.

It gives this error
{"success":false,"message":[["Invalid request in body."]],"result":[]}

this is a method that I want to send a request for account balance


       responseBody, _ := json.Marshal(map[string]string{
		"request": "/api/v4/main-account/balance",
		"nonce":   strconv.Itoa(helper.CreateTimestamp()),
	})
	payload := helper.ConvertToBase64(responseBody)
	key, secret := helper.ReadJSONFile(WHITEBIT)

	req, err := http.NewRequest("POST", WHITEBIT_WALLET, bytes.NewBuffer(responseBody))
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Set("Content-type", "application/json")
	req.Header.Set("X-TXC-APIKEY", key)
	req.Header.Set("X-TXC-PAYLOAD", payload)
	req.Header.Set("X-TXC-SIGNATURE", helper.CreateHMAC([]byte(payload), secret, WHITEBIT))

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}

	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	fmt.Println(string(body))

The helper package that I used for methods

func ConvertToBase64(data []byte) string {
	uEnc := b64.StdEncoding.EncodeToString(data)
	return uEnc
}

func CreateHMAC(data []byte, key string, exchange string) string {
	var hmacKey hash.Hash
	switch exchange {
	case "whitebit":
		hmacKey = hmac.New(sha512.New, []byte(key))
	case "bitfinex":
		hmacKey = hmac.New(sha512.New384, []byte(key))
	}
	hmacKey.Write(data)

	sha := fmt.Sprintf("%x", hmacKey.Sum(nil))

	return sha
}

func CreateTimestamp() int {
	return int(time.Now().Unix())
}