Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why there was different response on same request? #155

Open
yushoaqiang opened this issue Mar 14, 2019 · 6 comments
Open

why there was different response on same request? #155

yushoaqiang opened this issue Mar 14, 2019 · 6 comments

Comments

@yushoaqiang
Copy link

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

@JacobPlaster
Copy link
Contributor

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
Copy link
Author

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
Copy link
Author

this is my code,thank you!

@JacobPlaster
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Author

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"].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants