前提・実現したいこと
Golangを利用し、バイナンスのアカウントバランスへのアクセスを試みています。しかし、うまくアクセスできず、おそらくdoRequestの'resp'が上手く取れていないのですが解決方法が判明せず何かアイデアを頂ければ助かります!!
Binance api documentation: https://binance-docs.github.io/apidocs/spot/en/#change-log
go-binance json documentation: https://github.com/adshao/go-binance/blob/master/v2/account_service.go
発生している問題・エラーメッセージ
エラーメッセージ "url=/api/v3/account resp={"code":-2014,"msg":"API-key format invalid."}", "action=GetBalance err=json: cannot unmarshal object into Go value of type []Binance.Balance"
該当のソースコード
//apiKey = ' ' //apiSecret = ' ' package Binance import ( "bytes" "crypto/hmac" "crypto/sha256" "encoding/hex" "encoding/json" "io/ioutil" "log" "net/http" "net/url" "strconv" "time" ) const baseURL = "https://api.binance.com/" type APIClient struct { key string secret string httpClient *http.Client } func New(key, secret string) *APIClient { apiClient := &APIClient{key, secret, &http.Client{}} return apiClient } //header function that returns map[string]string which contains //access key, access timestamp, access sign, and content type func (api APIClient) header(method, endpoint string, body []byte) map[string]string { //define timestamp timestamp := strconv.FormatInt(time.Now().Unix(), 10) log.Println(timestamp) message := timestamp + method + endpoint + string(body) log.Println(message) // mac := hmac.New(sha256.New, []byte(api.secret)) mac.Write([]byte(message)) sign := hex.EncodeToString(mac.Sum(nil)) log.Println(api.key) log.Println(sign) return map[string]string{ "ACCESS-KEY": api.key, "ACCESS-TIMESTAMP": timestamp, "ACCESS-SIGN": sign, "Content-Type": "application/json", } } //request function using header function //return (body []byte, err error) func (api *APIClient) doRequest(method, urlPath string, query map[string]string, data []byte) (body []byte, err error) { //check url is correct baseURL, err := url.Parse(baseURL) if err != nil { return } apiURL, err := url.Parse(urlPath) if err != nil { return } endpoint := baseURL.ResolveReference(apiURL).String() log.Printf("action=doRequest endpoint=%s", endpoint) req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(data)) if err != nil { return } q := req.URL.Query() for key, value := range query { q.Add(key, value) } req.URL.RawQuery = q.Encode() for key, value := range api.header(method, req.URL.RequestURI(), data) { req.Header.Add(key, value) } resp, err := api.httpClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err = ioutil.ReadAll(resp.Body) if err != nil { return nil, err } return body, nil } // Account define account info type Account struct { MakerCommission int64 `json:"makerCommission"` TakerCommission int64 `json:"takerCommission"` BuyerCommission int64 `json:"buyerCommission"` SellerCommission int64 `json:"sellerCommission"` CanTrade bool `json:"canTrade"` CanWithdraw bool `json:"canWithdraw"` CanDeposit bool `json:"canDeposit"` Balances []Balance `json:"balances"` } // Balance define user balance of your account type Balance struct { Asset string `json:"asset"` Free string `json:"free"` Locked string `json:"locked"` } func (api *APIClient) GetBalance() ([]Balance, error){ url := "/api/v3/account" resp, err := api.doRequest("GET", url, map[string]string{}, nil) log.Println(resp) log.Printf("url=%s resp=%s", url, string(resp)) log.Println(resp) if err != nil { log.Printf("action=GetBalance err=%s", err.Error()) return nil, err } var balance []Balance err = json.Unmarshal(resp, &balance) log.Println(resp) if err != nil { log.Printf("action=GetBalance err=%s", err.Error()) return nil, err } return balance, nil }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。