java - 使用android studio的gdax rest api请求

标签 java android gdax-api

您好,我正在尝试使用 android studio 从 gdax 进行 rest api 调用,我是 rest 调用的新手,所以我很难进行此调用

我相信这是 api 端点,
Link 它说需要 CB-ACCESS-KEY header

这是所有必需 header 的列表

所有 REST 请求必须包含以下 header :

-CB-ACCESS-KEY 作为字符串的 api key 。

-CB-ACCESS-SIGN base64 编码的签名(参见消息签名)。

-CB-ACCESS-TIMESTAMP 请求的时间戳。

-CB-ACCESS-PASSPHRASE 您在创建 API key 时指定的密码。

-所有请求主体都应具有内容类型 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 密码,请帮忙

最佳答案

如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

你需要做点什么:

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 客户端库

关于java - 使用android studio的gdax rest api请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045153/

相关文章:

java - 具有层次结构的 JSR 303 验证

java - 是否可以同时使用线程消费者/生产者?

android - 无法解析符号 'permission' Android Studio

java - 安卓。没有找到启动器 Activity ?

r - GDAX API 与 R : invalid signature

java - 自定义 JComponent 不适用于流布局

java - 如何阻止 Eclipse 卡在很长的自动完成列表上?

android - Realm Android 和具有多面计算的大数据集

java - GDAX API 错误请求 400

node.js - GDAX-tt 上的 "typescript unexpected token import"- 尝试按照其他软件的其他修复无济于事