3

嗨,我正在尝试使用 android studio 从 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 密码,请帮忙

4

1 回答 1

3

正如api中提到的

CB-ACCESS-SIGN 标头是通过在 prehash 字符串时间戳 + 方法 + requestPath + 正文(其中 + 表示字符串连接)上使用 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 客户端库

于 2018-11-01T13:29:45.483 回答