0

I keep getting 401 error when trying to access an api, the api requires a username and password which i have given. I'm using jsonrpc2 library to make this all work. Here is my code:

App.java

 public class App {

public static JSONRPC2Session rpcSession;
private static final String MONERO_URL = "http://localhost:18082/json_rpc";
public static int requestId = 1;
public static JSONRPC2Request request;
public static JSONRPC2Response response;

public static void main(String[] args) throws MalformedURLException, JSONRPC2SessionException
{
  MoneroClient client = new MoneroClient(MONERO_URL); 
  client.getBalance();

}
}

MoneroClient.java

public class MoneroClient {

private int requestId = 0;

private JSONRPC2Session rpcSession;

public MoneroClient(String url) throws MalformedURLException {
    rpcSession = new JSONRPC2Session(new URL(url));

    rpcSession.setRawResponseInspector(new MyInspector());


    rpcSession.setConnectionConfigurator(new BasicAuthenticator());
} 
public BigDecimal getBalance() throws JSONRPC2SessionException {
    BigDecimal balance = null;
    JSONObject result = rpcCall("getbalance", null);
    if (result != null) {
        balance = BigDecimal.valueOf(Long.valueOf("" + result.get("balance")), 12);
        //balance = String.valueOf(result.get("balance"));
    }
    return balance;
}

private JSONObject rpcCall(String method, List<Object> params) throws JSONRPC2SessionException {
    JSONRPC2Request request = new JSONRPC2Request(method, params, ++requestId);
    JSONRPC2Response response = rpcSession.send(request);
    JSONObject result = null;
    if (response.indicatesSuccess()) {
        result = (JSONObject) response.getResult();
    } else {
        // TODO: Throw exception
        System.err.println(response.getError().getMessage());
    }
    return result;
}

}

BasicAuthenicator.java

public class BasicAuthenticator implements ConnectionConfigurator
{

    public void configure(HttpURLConnection connection) 
    {
        connection.addRequestProperty("Authorization", "Basic QWxhZGRpbjpPcGVuU2VzYW1l");
    }
}
HashTables
  • 392
  • 2
  • 7
  • 22
  • 1
    Is there an WWW-Authenticate header in the HTTP 401 response, and if it's set, what does it say? – Vladimir Dzhuvinov Jan 31 '17 at 06:32
  • No, how do i set it? – HashTables Jan 31 '17 at 09:01
  • Ok i found the www-authenicate header: WWW-Authenticate: Digest qop="auth",algorithm=MD5,realm="monero-wallet-rpc",nonce="ReWF4CrZipBfTtuaZPrpbw==",stale=false Digest qop="auth",algorithm=MD5-sess,realm="monero-wallet-rpc",nonce="ReWF4CrZipBfTtuaZPrpbw==",stale=false – HashTables Jan 31 '17 at 09:53
  • No idea. Try to find a library or a piece of code that can generate md5 digest authorization headers. In any case that's outside the scope of JSON-RPC 2.0. – Vladimir Dzhuvinov Feb 01 '17 at 10:59

0 Answers0