Statuscode 401 (Unauthorized)

+4 votes
I use the following code to access a multichain I have setup in AWS.

        // params
        String username = "lorem";
        String password = "ipsum";
        
        String server = "xxx.xxx.xxx.xxx";
        String port = "xxxx";
        String chainName = "chain1";
        
        // json
        Map<String,String> params = new HashMap<>();
        params.put("method", "getinfo");
        params.put("id", "1");
        params.put("chain_name", chainName);
        
        String jsonData = encode(params);
        System.out.println(jsonData);
        

        String login = username + ":" + password;
        String base64login = new String(Base64.getEncoder().encode(login.getBytes()));        
        
        // build request
        String url = "http://" + server + ":" + port;
        System.out.println(url);
        Document doc = Jsoup.connect(url)
            .header("Authorization", "Basic " + base64login)                
            .header("Accept", "application/json")
            .header("Accept-Encoding", "gzip,deflate,sdch")
            .header("Accept-Language", "en-EN,es;q=0.8")
            .header("Connection", "keep-alive")
            .header("X-Requested-With", "XMLHttpRequest")
            .data(jsonData, "")
            .post();
               
        // send
       System.out.println(doc.toString());

This code, when executed, throws a 401 error.

The multichain.conf file looks like this:
rpcallowip=0.0.0.0/0
rpcuser=lorem
rpcpassword=ipsum

(obviously I changed the username and password)

What am I doing wrong?
asked Jan 12, 2017 by Joris Schellekens

2 Answers

+1 vote

After setting rpcallowip, did you restart the MultiChain node?

Also, have you tried access it from multichain-cli running on the same computer as your Java code? You can use the rpcconnect (IP address), rpcport, rpcuser, rpcpassword command-line parameters for multichain-cli to connect to a remote instance of multichaind.

answered Jan 12, 2017 by MultiChain
Executing the multichaind command with those parameters just prompts the help message.
You need to use those parameters with multichain-cli not multichaind - please try running it first with no parameters, and the help will be shown.
+1 vote

I got the same 401 error. 

This is how I resolve this 401 problem:

https://github.com/HemingwayLee/multichain-cheatsheet


In short, there are 2 multichain.conf files.

I allow access by modifying ~/.multichain/multichain.conf

```
rpcallowip=0.0.0.0/0
rpcport=7434
```

The rpcuser and rpcpassword will be generated automatically in ~/.multichain/chain1/multichain.conf, for example:

```
rpcuser=multichainrpc
rpcpassword=j3536YzAeJMXRZXLkt94bqmeWYWaKGNjETtDwAN2w6T
```

After putting rpcuser and rpcpassword to Basic Auth. 
I am able to avoid this 401 error.
answered Sep 29, 2018 by ywlee
edited Sep 29, 2018 by ywlee
...