Post
Topic
Board Marketplace
Re: Bitcoinica - Advanced Bitcoin Trading Platform
by
notme
on 16/04/2012, 21:11:29 UTC
Hey zhou, I was trying to write a Java application to use with the Bitcoinica API, but I seem to be having trouble with the authentication? I can put in https://username:password@www.bitcoinica.com/api/orders.json?n=5 in my browser and it works fine, but I get an Unauthorized exception thrown when I try it in my application. My application grabs all data that does not require authentication just fine, but anything that requires it throws exceptions. Any idea what is the problem?

This is the code I'm using to request the data.

public String getSomething(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn =
            (HttpURLConnection) url.openConnection();

        if (conn.getResponseCode() != 200) {
          throw new IOException(conn.getResponseMessage());
        }

        // Buffer the result into a string
        BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
          sb.append(line);
        }
        rd.close();

        conn.disconnect();
        return sb.toString();
    }

I'm no Java expert, but it sounds like your HTTP library doesn't handle un/pw in the url like you are expecting.

A quick googling yields this, which may help:
http://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java