<p>I think this is related to this open issue. </p>

<p><a href="https://github.com/bitcoin/bitcoin/issues/9661" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/issues/9661</a></p>

<p>I was running bitcoind on Ubuntu 16.</p>

<p>I had the similar issue when I had <code>rpcuser</code> and <code>rpcpassword</code> in the config file. All I got is 401 error when I called the jaon-rpc api.</p>

<p>I found a workaround by providing <code>-rpcuser=REPLACED -rpcpassword=REPLACED</code> when I launched the bitcoind.</p>

<p>Hope this is useful.</p>

<p>One more thing, instead of debug from your rpc client in PHP. Maybe try it with <code>curl</code> first. HTTP 401 clearly is an authentication issue, using <code>curl</code> is much easier for debugging. </p>

CC BY-SA 3.0
2018-01-17T17:52:45.050Z
2018-01-17T17:52:45.050Z
Chu Xiwen
<p>Please make sure that your RPC username and password are correct, also make 
sure that your username and password don't contain (#) tag. </p>

CC BY-SA 4.0
2018-12-02T04:13:25.507Z
2018-12-02T04:13:25.507Z
Tailer
<p>Make sure to check which <code>bitcoin.conf</code> configuration file the <code>bitcoind</code> daemon is using. Its explicitly shown on bootstrapping.</p>

<p>This would arise if you change default bitcoin-core installation path on setup.</p>

<p>This way you won't need to pass the user/pwd via cli on service start.</p>

CC BY-SA 4.0
2019-03-04T08:03:57.220Z
2019-03-04T08:03:57.220Z
behkod
<p>When I execute my php file from the command line I get this:</p>

<pre><code>&lt;pre&gt;
PHP Warning:  fopen(http://...@127.0.0.1:8332/): failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization Required
 in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 132
PHP Fatal error:  Uncaught exception 'Exception' with message 'Unable to connect to http://b:a@127.0.0.1:8332/' in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php:140
Stack trace:
#0 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient-&gt;__call('getinfo', Array)
#1 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient-&gt;getinfo()
#2 {main}
  thrown in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 140
</code></pre>

<hr>

<p>My php file looks like this:</p>

<pre><code>&lt;?php
  require_once 'jsonRPCClient.php';

  $bitcoin = new jsonRPCClient('http://user:password@127.0.0.1:8332/',true);
  #$param = array(
  #'c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd',
  #'1');

  #$return1 = $bitcoin-&gt;getrawtransaction('c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd','1')

  echo "&lt;pre&gt;\n";
  print_r($bitcoin-&gt;getinfo()); echo "\n";
  #print_r($return1['0']); echo "\n";
  #echo "Received: ".$bitcoin-&gt;getreceivedbylabel("Your Address")."\n";
  echo "&lt;/pre&gt;";
?&gt;
</code></pre>

<hr>

<p>My webpage with apache2 appears blank that uses the php file I made.
I am using the -txindex parameter when I launch bitcoind.</p>

<p>But I am able to use bitcoin-cli to interact with bitcoind</p>

<pre><code>Iv'e used:
bitcoin-cli getrawtransaction c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd 1

and:
bitcoin-cli getinfo
</code></pre>

<hr>

<p>Also my bitcoin.conf looks like this:</p>

<pre><code>rpcuser=user
rpcpassword=pass
txindex=1
server=1
</code></pre>

<hr>

<p>Also note that I substituted the usernames and passwords to hide them.</p>

<p>My question is why can I use bitcoin-cli and get no error, but when I try to use the jsonRPCClient.php I get the error 401 Authorization Required and how do I fix the error?</p>

CC BY-SA 3.0
2015-04-17T18:28:36.787Z
Error 401 means bad user/password. I notice that the password is `pass` in your config file, and it's `password` in your config file. Does that reflect the contents of your config/php files?
Nick ODell
CC BY-SA 3.0
2015-04-17T23:48:53.523Z
No, sorry for the confusion, I replaced my password and username listed on this forum to maintain security of the bitcoin core on my computer. The password and username I used on my computer match with those in the config file and I have checked many times.
Gemohn Herbile
CC BY-SA 3.0
2015-04-17T23:59:05.597Z
I dunno what to tell you. [Bad user/pass](https://github.com/bitcoin/bitcoin/blob/4c4f1b4721c7c8ef54eb5eea0208482352503d24/src/rpcserver.cpp#L917) is what triggers the error.
Nick ODell
CC BY-SA 3.0
2015-04-18T13:12:42.213Z
Have you restarted your client since putting the username and password in the conf file?
morsecoder
CC BY-SA 3.0
2015-04-18T13:26:04.633Z
I think so, I have re-started my php script and bitcoind as well, and the wierd thing is that it worked at one point.
Gemohn Herbile
CC BY-SA 3.0
2015-07-05T06:43:17.867Z
I've now discovered its partially caused by leaving armory bitcoin wallet manager open for days.
Gemohn Herbile
<bitcoind><client><json-rpc>
jsonRPCClient.php states its served a 401 error response
Gemohn Herbile
# jsonRPCClient.php states its served a 401 error response

(Asked by: username_0 on 2015-04-17)

When I execute my php file from the command line I get this:

PHP Warning:  fopen(http://...@127.0.0.1:8332/): failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization Required
 in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 132
PHP Fatal error:  Uncaught exception 'Exception' with message 'Unable to connect to http://b:a@127.0.0.1:8332/' in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php:140
Stack trace:
#0 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient->__call('getinfo', Array)
#1 /home/brian/Desktop/VH2_apache2/host02/btc16/index1.php(12): jsonRPCClient->getinfo()
#2 {main}
  thrown in /home/brian/Desktop/VH2_apache2/host02/btc16/jsonRPCClient.php on line 140


My php file looks like this:
getrawtransaction('c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd','1')

  echo "\n";
  print_r($bitcoin->getinfo()); echo "\n";
  #print_r($return1['0']); echo "\n";
  #echo "Received: ".$bitcoin->getreceivedbylabel("Your Address")."\n";
  echo "";
?>


My webpage with apache2 appears blank that uses the php file I made.
I am using the -txindex parameter when I launch bitcoind.
But I am able to use bitcoin-cli to interact with bitcoind
Iv'e used:
bitcoin-cli getrawtransaction c418518d5ffccd947e377ab7e589f684d83e6df1af8f397cc11848ac8a78f0dd 1

and:
bitcoin-cli getinfo


Also my bitcoin.conf looks like this:
rpcuser=user
rpcpassword=pass
txindex=1
server=1


Also note that I substituted the usernames and passwords to hide them.
My question is why can I use bitcoin-cli and get no error, but when I try to use the jsonRPCClient.php I get the error 401 Authorization Required and how do I fix the error?

**Tags:** bitcoind, client, json-rpc

> **Comments:**
> **username_1**: Error 401 means bad user/password. I notice that the password is `pass` in your config file, and it's `password` in your config file. Does that reflect the contents of your config/php files?  
> **username_0**: No, sorry for the confusion, I replaced my password and username listed on this forum to maintain security of the bitcoin core on my computer. The password and username I used on my computer match with those in the config file and I have checked many times.  
> **username_1**: I dunno what to tell you. [Bad user/pass](https://github.com/bitcoin/bitcoin/blob/4c4f1b4721c7c8ef54eb5eea0208482352503d24/src/rpcserver.cpp#L917) is what triggers the error.  
> **username_2**: Have you restarted your client since putting the username and password in the conf file?  
> **username_0**: I think so, I have re-started my php script and bitcoind as well, and the wierd thing is that it worked at one point.  
> **username_0**: I've now discovered its partially caused by leaving armory bitcoin wallet manager open for days.  

---

## 3 answers

### Answer by username_3 on 2018-01-17. Score: 2

I think this is related to this open issue.

<https://github.com/bitcoin/bitcoin/issues/9661>

I was running bitcoind on Ubuntu 16.

I had the similar issue when I had `rpcuser` and `rpcpassword` in the config file. All I got is 401 error when I called the jaon-rpc api.

I found a workaround by providing `-rpcuser=REPLACED -rpcpassword=REPLACED` when I launched the bitcoind.

Hope this is useful.

One more thing, instead of debug from your rpc client in PHP. Maybe try it with `curl` first. HTTP 401 clearly is an authentication issue, using `curl` is much easier for debugging.


---

### Answer by username_4 on 2018-12-02. Score: 0

Please make sure that your RPC username and password are correct, also make
sure that your username and password don't contain (#) tag.


---

### Answer by username_5 on 2019-03-04. Score: 0

Make sure to check which `bitcoin.conf` configuration file the `bitcoind` daemon is using. Its explicitly shown on bootstrapping.

This would arise if you change default bitcoin-core installation path on setup.

This way you won't need to pass the user/pwd via cli on service start.


---

