2014-12-03 9 views
1

我正在使用Coinbase API,請visit here瞭解有關此API的更多信息。缺少必需的參數:帳戶

所以,我運行這個程序,你可以看看,但它說:

{"success":false,"errors":["Required parameter missing: account"]} 

這裏是我的程序:

import java.io.IOException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 

import javax.crypto.Mac; 
import javax.crypto.spec.SecretKeySpec; 

import org.apache.commons.codec.binary.Hex; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.methods.HttpRequestBase; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.util.EntityUtils; 

import com.coinbase.api.Coinbase; 
import com.coinbase.api.CoinbaseBuilder; 

public class TransactionExample { 

    static String API_KEY = "MY API KEY"; 

    static String API_SECRET = "MY API SECRET"; 

    public static String getHttp(String url, String body) 
      throws InvalidKeyException, NoSuchAlgorithmException, 
      ClientProtocolException, IOException { 

     String nonce = String.valueOf(System.currentTimeMillis()); 
     String message = nonce + url + (body != null ? body : ""); 

     Mac mac = Mac.getInstance("HmacSHA256"); 
     mac.init(new SecretKeySpec(API_SECRET.getBytes(), "HmacSHA256")); 
     String signature = new String(Hex.encodeHex(mac.doFinal(message.getBytes()))); 

     HttpRequestBase request; 
     if (body == null || body.length() == 0) 
      request = new HttpGet(url); 
     else { 
      HttpPost post = new HttpPost(url); 
      post.setEntity(new StringEntity(body)); 
      request = post; 
     } 
     request.setHeader("ACCESS_KEY", API_KEY); 
     request.setHeader("ACCESS_SIGNATURE", signature); 
     request.setHeader("ACCESS_NONCE", nonce); 

     HttpClient httpClient = HttpClientBuilder.create().build(); 
     HttpResponse response = httpClient.execute(request); 

     HttpEntity entity = response.getEntity(); 
     if (entity != null) 
      return EntityUtils.toString(entity); 
     return null; 
    } 

    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, ClientProtocolException, IOException{ 

     System.out.println(getHttp("https://api.coinbase.com/v1/accounts", "{'account':{'name':'Savings Wallet'}}")); 
    } 
} 

請幫幫忙!

當然,幫助將不勝感激!

回答

0

你需要換出佔位符文本這裏您的實際API密鑰:

static String API_KEY = "MY API KEY"; 

static String API_SECRET = "MY API SECRET"; 

你可以在https://www.coinbase.com/settings/api得到它們。

雖然如果剛開始時,我建議先使用 API進行測試,以避免任何安全風險。如果您的API密鑰有權購買/出售或從您的帳戶中匯出資金,任何擁有這些密鑰的人都可以這樣做。

使用Sandbox API時,您需要將URL從https://api.coinbase.com/v1/accounts更改爲https://api.sandbox.coinbase.com/v1/accounts

此外,我們發佈了v2 of our API,這更加棒,但需要對此腳本進行其他修改。