Reputation: 9185
I am using the Java library for JSON-RPC 2.0 from dzhuvinov. I am having problems setting my username and password for basic access authenticaition for the calls. My code looks like:
public static void main(String[] args)
{
URL serverURL = null;
try {
serverURL = new URL("http://user:[email protected]:18332/");
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
return;
}
JSONRPC2Session mySession = new JSONRPC2Session(serverURL);
String method = "getinfo";
int requestID = 0;
JSONRPC2Request request = new JSONRPC2Request(method, requestID);
JSONRPC2Response response = null;
try {
response = mySession.send(request);
} catch (JSONRPC2SessionException e) {
System.err.println(e.getMessage());
return;
}
if (response.indicatesSuccess())
System.out.println(response.getResult());
else
System.out.println(response.getError().getMessage());
}
And I get a response of:
Network exception: Server returned HTTP response code: 401 for URL: http://user:[email protected]:18332/
Trying a similar code in Python I get a proper result:
access = jsonrpc.ServiceProxy("http://user:[email protected]:18332/")
print access.getinfo()
How can I configure the basic access authentication for my JSON RPC calls?
Upvotes: 1
Views: 2651
Reputation: 920
Not sure how library from 'dzhuvinov' deals with streams. But you can apply this code to perform basic authentication on HTTP-transport level opened by URL
class.
URL url = new URL("http://user:[email protected]:18332/");
URLConnection conn = url.openConnection();
String userpass = rpcuser + ":" + rpcpassword;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
conn.setRequestProperty ("Authorization", basicAuth);
conn.setRequestProperty ("Content-Type", "application/json");
conn.setDoOutput(true);
Full code can be found here http://engnick.blogspot.ru/2013/03/java-trac-rpc-and-json.html . I used this to perform JSON-RPC stuff to communicate with API of Trac
.
Upvotes: 1
Reputation: 308
final String rpcuser ="...";
final String rpcpassword ="...";
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (rpcuser, rpcpassword.toCharArray());
}});
Upvotes: 1