bitfinexcom / bitfinex-api-go

BITFINEX Go trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
309 stars 220 forks source link

why there was different response on same request? #155

Open yushoaqiang opened 5 years ago

yushoaqiang commented 5 years ago

are you bitfinex developer? I called v2 api, positions. sometimes it return sucess,sometimes it return error:invalid key.

JacobPlaster commented 5 years ago

Hi @yushoaqiang please could you post your code so we can try to reproduce the problem (for obvious reasons do not expose your api keys)

yushoaqiang commented 5 years ago

func getPositions(client *http.Client){ apiUrl := "https://api.bitfinex.com/v2/auth/r/positions?"

nonce := fmt.Sprintf("%d", time.Now().UnixNano())
apikey := ""
secKey := ""

p := "/api/v2/auth/r/positions" + nonce +"{}"

sign, _ := GetParamHmacSha384Sign(secKey, p)

resp, err := NewHttpRequest(client, "POST", apiUrl, "{}", map[string]string{
    "Content-Type":    "application/json",
    "Accept":          "application/json",
    "bfx-nonce":    nonce,
    "bfx-apikey":   apikey,
    "bfx-signature": sign})

if err != nil {
    log.Println("NewHttpRequest err ", err)
}else{
    log.Println("resp", string(resp))
}

}

func GetParamHmacSha384Sign(secret, params string) (string, error) { mac := hmac.New(sha512.New384, []byte(secret)) _, err := mac.Write([]byte(params)) if err != nil { return "", nil } return hex.EncodeToString(mac.Sum(nil)), nil }

func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error) { req, err := http.NewRequest(reqType, reqUrl, strings.NewReader(postData)) if err!=nil{ fmt.Println(err) } req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")

if requstHeaders != nil {
    for k, v := range requstHeaders {
        req.Header.Add(k, v)
    }
}

resp, err := client.Do(req)
if err != nil {
    return nil, err
}
defer resp.Body.Close()

bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return nil, err
}
if resp.StatusCode != 200 {
    return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode, string(bodyData)))
}
return bodyData, nil

}

yushoaqiang commented 5 years ago

this is my code,thank you!

JacobPlaster commented 5 years ago

Thank @yushoaqiang. There may be a problem with the signing of the request here, could you please try using the built in rest client since that handles all of the authentication for you. Here is an example which achieves the same result as what you are attempting above:

package main

import (
    "flag"
    "log"
    "os"
    "github.com/bitfinexcom/bitfinex-api-go/v2/rest"
    "github.com/davecgh/go-spew/spew"
)

func main() {
    apikey := ""
    secKey := ""
    c := rest.NewClient().Credentials(key, secret)

    psoitions, err := c.Positions.All()

    if err != nil {
        log.Fatalf("getting wallet %s", err)
    }

    spew.Dump(positions)
}

Let me know how that turns out!

JacobPlaster commented 5 years ago

Also if there is a specific reason as to why you want to handle the signature generation then please refer to the signing method in the library which can be found here: https://github.com/bitfinexcom/bitfinex-api-go/blob/master/v2/rest/client.go#L122

yushoaqiang commented 5 years ago

I have to use a proxy to complete the http request, but it's too hard in your library. I try to change source code to set the proxy. But it always return ["error",10100,"apikey: invalid"].