hi试图使用android studio从gdax进行rest api调用,这是rest调用的新手,所以我正努力进行此调用
我认为这是api端点,
Link
它说需要CB-ACCESS-KEY标头
这是所有必需标头的列表
所有REST请求必须包含以下标头:
-CB-ACCESS-KEY api键作为字符串。
-CB-ACCESS-SIGN base64编码的签名(请参阅对消息签名)。
-CB-ACCESS-TIMESTAMP您请求的时间戳。
-CB-ACCESS-PASSPHRASE创建API密钥时指定的密码。
-所有请求正文均应具有内容类型application / json并为有效JSON。
链接到完整文档click here
这是我要运气不佳的代码
private class InfoTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
System.out.println("oooooooooooooooooooook working2");
HttpURLConnection conn = null;
BufferedReader reader = null;
try{
String query = urls[0];
URL url = new URL(endpoint+query);
System.out.println(url);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("CB-ACCESS-KEY", key);
// conn.setRequestProperty("CB-ACCESS-SIGN", generate(params[0], "GET", "", String.valueOf(System.currentTimeMillis())));
String timestamp = String.valueOf(System.currentTimeMillis());
conn.setRequestProperty("CB-ACCESS-TIMESTAMP", timestamp);
conn.setRequestProperty("CB-ACCESS-PASSPHRASE", passprase);
Writer writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(query);
writer.flush();
writer.close();
conn.connect();
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
sb.append(line);
}
return sb.toString();
}catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
TextView t = findViewById(R.id.t);
t.setText(result);
}
}
我正在从我的onCreate调用此任务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new InfoTask().execute("accounts");
}
我不确定用于CB-ACCESS-SIGN的参数是什么,也不知道在哪里添加我的api机密,请帮忙
答案 0 :(得分:2)
如api
所述CB-ACCESS-SIGN标头是通过使用以下命令创建sha256 HMAC生成的 prehash字符串timestamp +方法上的base64解码密钥 + requestPath +正文(其中+表示字符串连接),并对输出进行base64编码。时间戳记值与 CB-ACCESS-TIMESTAMP标头
您需要做一些事情:
public String generate(String requestPath, String method, String body, String timestamp) {
try {
String prehash = timestamp + method.toUpperCase() + requestPath + body;
byte[] secretDecoded = Base64.getDecoder().decode(secretKey);
SecretKeySpec keyspec = new SecretKeySpec(secretDecoded, GdaxConstants.SHARED_MAC.getAlgorithm());
Mac sha256 = (Mac) GdaxConstants.SHARED_MAC.clone();
sha256.init(keyspec);
return Base64.getEncoder().encodeToString(sha256.doFinal(prehash.getBytes()));
} catch (CloneNotSupportedException | InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeErrorException(new Error("Cannot set up authentication headers."));
}
}
另一种方法是使用gdax-java,这是gdax的Java客户端库