[英]gdax rest api request using android studio
嗨我正在嘗試使用Android工作室從gdax進行休息api調用,我是新來的休息電話,所以我很難打這個電話
我相信這是api端點,
鏈接它說CB-ACCESS-KEY標頭是必需的
這是所有必需標題的列表
所有REST請求必須包含以下標頭:
-CB-ACCESS-KEY api鍵作為字符串。
-CB-ACCESS-SIGN base64編碼的簽名(請參閱簽名消息)。
-CB-ACCESS-TIMESTAMP您的請求的時間戳。
-CB-ACCESS-PASSPHRASE創建API密鑰時指定的密碼。
- 所有請求主體應具有內容類型application / json並且是有效的JSON。
鏈接到完整文檔單擊此處
這是我試圖使用的代碼沒有運氣
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秘密請幫忙
如api中所述
CB-ACCESS-SIGN標頭是通過在prehash字符串時間戳+ method + requestPath + body(其中+表示字符串連接)上使用base64解碼的密鑰創建sha256 HMAC並對輸出進行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客戶端庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.