3

hi am trying to make a rest api call from gdax using android studio, am new to rest calls so i am struggling to make this call

i believe this is the api endpoint,
Link it says CB-ACCESS-KEY header is required

here is a list of all the required headers

All REST requests must contain the following headers:

-CB-ACCESS-KEY The api key as a string.

-CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message).

-CB-ACCESS-TIMESTAMP A timestamp for your request.

-CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key.

-All request bodies should have content type application/json and be valid JSON.

link to full document click here

here is the code i am trying to use with no luck

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 am calling this task from my onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new InfoTask().execute("accounts");
}

i am not sure what parameters to use for the CB-ACCESS-SIGN and also don't know where to add my api secret please help

user2692997
  • 2,001
  • 2
  • 14
  • 20

1 Answers1

3

As mentioned in 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."));
        }
    }

Also another way is use gdax-java, this is java client library for gdax

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45