简体   繁体   English

使用android studio的gdax rest api请求

[英]gdax rest api request using android studio

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 嗨我正在尝试使用Android工作室从gdax进行休息api调用,我是新来的休息电话,所以我很难打这个电话

i believe this is the api endpoint, 我相信这是api端点,
Link it says CB-ACCESS-KEY header is required 链接它说CB-ACCESS-KEY标头是必需的

here is a list of all the required headers 这是所有必需标题的列表

All REST requests must contain the following headers: 所有REST请求必须包含以下标头:

-CB-ACCESS-KEY The api key as a string. -CB-ACCESS-KEY api键作为字符串。

-CB-ACCESS-SIGN The base64-encoded signature (see Signing a Message). -CB-ACCESS-SIGN base64编码的签名(请参阅签名消息)。

-CB-ACCESS-TIMESTAMP A timestamp for your request. -CB-ACCESS-TIMESTAMP您的请求的时间戳。

-CB-ACCESS-PASSPHRASE The passphrase you specified when creating the API key. -CB-ACCESS-PASSPHRASE创建API密钥时指定的密码。

-All request bodies should have content type application/json and be valid JSON. - 所有请求主体应具有内容类型application / json并且是有效的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 我从我的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 我不知道用于CB-ACCESS-SIGN的参数是什么,也不知道在哪里添加我的api秘密请帮忙

As mentioned in 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. CB-ACCESS-SIGN标头是通过在prehash字符串时间戳+ method + requestPath + body(其中+表示字符串连接)上使用base64解码的密钥创建sha256 HMAC并对输出进行base64编码来生成的。 The timestamp value is the same as the CB-ACCESS-TIMESTAMP header 时间戳值与CB-ACCESS-TIMESTAMP头相同

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 另一种方法是使用gdax-java ,这是用于gdax的java客户端库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM