1

I'm quite new in coding and was hoping I get an advice how to solve my problem. I'm writing a java code to connect to trade.io API, but I can't figure out how to submit the correct encrypted message to the exchange using "POST" and "DELETE".

So far, I managed to work out how to receive information with "GET", but no success on the others.

Here is what I wrote so far:

/*
* CancelOrder cancels an existing order ==> This doesn't work!
*/
    public String CancelOrder(String orderId) throws MalformedURLException, 
        IOException {

        return signAndSend("/order/" + orderId, "DELETE");
    }   


     /* 
     * Reads the open orders in the account ==> This works!
     */
    public String getOpenOrders(String symbol) throws MalformedURLException, 
        IOException {
        return signAndSend("/openOrders/" + symbol, "GET");
    }


    /*
     * Signs and Sends signature. This method is called when signature is 
      needed.
     */
    private String signAndSend(String url, String method) throws 
    MalformedURLException, IOException {

        String nonce = String.valueOf(System.currentTimeMillis());
        String baseUrl = UrlTradeio.urlV1;
        String ts = "?ts=" + nonce;

        String sign = hmac512Digest(ts, TRADEIO_SECRET_KEY).toUpperCase();

        HttpURLConnection con = (HttpURLConnection) new URL(baseUrl + url + ts).openConnection();
        con.setRequestProperty("Sign", sign);
        con.setRequestProperty("Key", TRADEIO_API_KEY);
        con.setRequestMethod(method);
        con.connect();

        InputStream response = con.getInputStream();
//
        try (Scanner scanner = new Scanner(response)) {

            String responseBody = scanner.next();

            return responseBody;
        }

    }

The exchange provided very thorough example in C# here:

https://github.com/tradeio/api-csharpclient/blob/master/Tradeio.Client/TradeioApi.cs

Here is the output for "getOpenOrders" and the error message when I try to close the same.

ktos_eth open orders are: {"code":0,"timestamp":1559338453064,"orders":[{"orderId":"-72057593948251267","total":"0.0","orderType":"limit","commission":"0.0","createdAt":"2019-01-23T17:36:55.8633777Z","unitsFilled":"0.0","isPending":true,"status":"Working","type":"sell","requestedAmount":"75.0","baseAmount":"0.0","quoteAmount":"0.0","price":"0.00014000","isLimit":true,"loanRate":"0.0","rateStop":"0.0","instrument":"ktos_eth","requestedPrice":"0.00014000","remainingAmount":"75.0"}]}

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.exchange.trade.io/api/v1/order/-72057593948251267?ts=1559338452695 at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)

Thanks in advance for looking into it

Cheers Alex

Aleks
  • 23
  • 6

1 Answers1

0

So - 403 means you authenticated properly and were known by the system - but the system determined you lacked the permissions. Essentially it found you as a user - but said you didn't have access to what you wanted to do. More info on status codes here https://httpstatuses.com/

Having worked with these API keys before - I wonder if you don't have the API key set to allow you to perform the actions that are giving you a 403.

I recommend reviewing the API key you created on the crypto site. Make sure that not only do you have a key - but you have enabled that key to be used with the actions you would like. It looks like trade.io calls them "Permissions" and can be reviewed here https://trade.io/en/api.

My guess is you only have "Read Access" enabled for that key, not "Trading".

And welcome to Stack Overflow :-) .

K13
  • 124
  • 9
  • 1
    You are right, I regenerated the keys and the DELETE functions worked. I still face a problem submitting data with the "POST" method. It was returning an error 411 (content length) I tried to solve it by adding: `con.setDoOutput(true); con.setFixedLengthStreamingMode(6);` Now I have a error Insuffiecient Data Written – Aleks Jun 01 '19 at 10:29
  • Here is what I tried: `public String PlaceOrder(String symbol, String side, String type, String quantity, String price) throws MalformedURLException, IOException { LinkedHashMap form = new LinkedHashMap(); form.put("Symbol", symbol); form.put("Side", side); form.put("Type", type); form.put("Quantity", quantity); form.put("Price", price); return signAndSend("/order", form, "POST"); } ` – Aleks Jun 01 '19 at 10:35
  • First - if my answer solved your problem - then mark it as the solution so I get credited for my time. If you now have a totally new issue - consider making a new question. – K13 Jun 01 '19 at 14:38
  • But - a simple google search of a 411 error gives things u should try - https://stackoverflow.com/questions/15619562/getting-411-length-required-after-a-put-request-from-http-client – K13 Jun 01 '19 at 14:39
  • something like “conn.setRequestProperty("Content-Length", "0");” perhaps ? Or if you are sending data - something like “data.length()” instead of 0. – K13 Jun 01 '19 at 14:46
  • 1
    I closed the thread, thanks for you reply. it helped to solved at least half the problem. I tried all I could think of for the POST function, dead end there. Cheers, Aleks – Aleks Jun 01 '19 at 16:25
  • Don’t give up. Take a day off, look at it with a fresh mind later. Post a new question with more details if you are stuck. This is learning :-). – K13 Jun 01 '19 at 17:26