Use Android Studio’s GDAX REST API requests
Hello, I’m trying to make a REST api call from GDAX using android studio, I’m new to REST calling, so it’s hard for me to make this call
I believe this is the api endpoint
Link
IT SAYS IT NEEDS THE CB-ACCESS-KEY header
This is a list of all required headers
All REST requests must contain the following header:
–
CB-ACCESS-KEY AS THE API KEY FOR THE STRING.
–
CB-ACCESS-SIGN base64 encoded signature (see Message signature).
–
CB-ACCESS-TIMESTAMP The timestamp of the request.
–
CB-ACCESS-PASSPHRASE The password you specified when you created the api key.
– All request bodies should have the content type application/json and be valid JSON.
Full documentation link click here
This is the code I tried to use but was unlucky
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);
}
}
I call this task in my onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new InfoTask().execute("accounts");
}
I’M NOT SURE WHAT PARAMETERS TO USE FOR CB-ACCESS-SIGN AND DON’T KNOW WHERE TO ADD MY API PASSWORD, PLEASE HELP
Solution
As described in the API
The CB-ACCESS-SIGN header is generated by creating a sha256 HMAC using
the base64-decoded secret key on the prehash string timestamp + method
+ requestPath + body (where + represents string concatenation) and base64-encode the output. The timestamp value is the same as the
CB-ACCESS-TIMESTAMP header
You need to do something :
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."));
}
}
Another approach is to use gdax-java, which is gdax’s java client library