Bitcoin Forum
May 07, 2024, 07:00:06 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Authentication, JSON RPC and Python  (Read 12847 times)
vess (OP)
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile WWW
August 03, 2010, 03:49:49 PM
 #1

Hi all,

Does anyone have jsonrpc or something similar working with HTTP Authentication? I'm trying to hit up my bitcoin server with python over JSON-RPC, and of course, getting authorization errors from the new server.

I couldn't find much about this online, surprisingly, so thought I'd ask here.

I'm the CEO of CoinLab (www.coinlab.com) and the Executive Director of the Bitcoin Foundation, I will identify if I'm speaking for myself or one of the organizations when I post from this account.
You can see the statistics of your reports to moderators on the "Report to moderator" pages.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715108406
Hero Member
*
Offline Offline

Posts: 1715108406

View Profile Personal Message (Offline)

Ignore
1715108406
Reply with quote  #2

1715108406
Report to moderator
1715108406
Hero Member
*
Offline Offline

Posts: 1715108406

View Profile Personal Message (Offline)

Ignore
1715108406
Reply with quote  #2

1715108406
Report to moderator
1715108406
Hero Member
*
Offline Offline

Posts: 1715108406

View Profile Personal Message (Offline)

Ignore
1715108406
Reply with quote  #2

1715108406
Report to moderator
vess (OP)
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile WWW
August 03, 2010, 05:20:44 PM
 #2

Update: Here is the current state of my hackish code:

postdata = jsonrpc.dumps({"method": 'getbalance', "params":'','id':'jsonrpc'})

req = urllib2.Request('http://127.0.0.1:8332', postdata)
      

base64string = base64.encodestring( '%s:%s' % ('username','rpcpassword'))[:-1]
authheader =  "Basic %s ." % base64string
req.add_header("Authorization",authheader)
      
handle = urllib2.urlopen(req)
      
print handle.read()


If username or password is wrong, then I get an Authorization error from the server as expected.

Otherwise, I get nothing, just a closed connection.

Any leads or tips?

I'm the CEO of CoinLab (www.coinlab.com) and the Executive Director of the Bitcoin Foundation, I will identify if I'm speaking for myself or one of the organizations when I post from this account.
nelisky
Legendary
*
Offline Offline

Activity: 1540
Merit: 1001


View Profile
August 03, 2010, 05:42:51 PM
 #3

Did you see http://bitcointalk.org/index.php?topic=528.msg4923#msg4923 ? That might be the problem... I had read somewhere about python urllib and bitcoing not playing along, but curl had no issues. Can't find that thread though.
vess (OP)
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile WWW
August 03, 2010, 06:02:00 PM
 #4

Thanks for the pointer, interesting, but not what seems to be affecting me.

Here's my current code (running on Google App Engine)

postdata = jsonrpc.dumps({"method": 'getbalance', "params":'','id':'jsonrpc'})
req = urllib2.Request('http://127.0.0.1:8332', postdata)
userpass = 'user:a'.encode('base64')[:-1]
authheader =  "Basic %s" % userpass
req.add_header("Authorization",authheader)
handle = urllib2.urlopen(req)
json_response = handle.read()
self.response.out.write (json_response)

This yields a HTTPError: HTTP Error 500: Internal Server Error

from the GAE local python script.


using
   postdata = jsonrpc.dumps([{"jsonrpc": "2.0","method": 'getbalance', "params":'','id':'1'}])

Yields the same result.

I'm the CEO of CoinLab (www.coinlab.com) and the Executive Director of the Bitcoin Foundation, I will identify if I'm speaking for myself or one of the organizations when I post from this account.
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
August 03, 2010, 06:09:08 PM
 #5

Thanks for the pointer, interesting, but not what seems to be affecting me.

Here's my current code (running on Google App Engine)

postdata = jsonrpc.dumps({"method": 'getbalance', "params":'','id':'jsonrpc'})
req = urllib2.Request('http://127.0.0.1:8332', postdata)
userpass = 'user:a'.encode('base64')[:-1]
authheader =  "Basic %s" % userpass
req.add_header("Authorization",authheader)
handle = urllib2.urlopen(req)
json_response = handle.read()
self.response.out.write (json_response)

This yields a HTTPError: HTTP Error 500: Internal Server Error

This is a verified bug in bitcoin.

bitcoin requires the Content-Length header, but several JSON-RPC libraries do not provide it.  When the Content-Length header is absent, bitcoin returns 500 Internal Server Error.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
vess (OP)
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile WWW
August 03, 2010, 06:43:29 PM
 #6

What you say is true, but urllib2 does send Content Length.

In my case, we had a datatypes problem which Gavin helped me find: params needs to get a list in python, so

"params" : []

will work.
"params" : ""

did not.

With that amendation, my second sample code works well now. Hope it's helpful to others.

I'm the CEO of CoinLab (www.coinlab.com) and the Executive Director of the Bitcoin Foundation, I will identify if I'm speaking for myself or one of the organizations when I post from this account.
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2216


Chief Scientist


View Profile WWW
August 03, 2010, 06:56:44 PM
 #7

bitcoin requires the Content-Length header, but several JSON-RPC libraries do not provide it.  When the Content-Length header is absent, bitcoin returns 500 Internal Server Error.
Can you be more specific about which JSON libraries don't provide Content-Length ?  It'd be nice to document that.


How often do you get the chance to work on a potentially world-changing project?
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
August 03, 2010, 06:58:58 PM
 #8

bitcoin requires the Content-Length header, but several JSON-RPC libraries do not provide it.  When the Content-Length header is absent, bitcoin returns 500 Internal Server Error.
Can you be more specific about which JSON libraries don't provide Content-Length ?  It'd be nice to document that.

The two JSON RPC libs available at CPAN (Perl), and a compliant C lib that I wrote locally to verify the behavior.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
aceat64
Full Member
***
Offline Offline

Activity: 307
Merit: 102



View Profile
August 03, 2010, 07:56:32 PM
 #9

Another issue I noticed with bitcoind's JSON-RPC is that by default there is no user, and PHP's fopen() function does not try to send the authentication information if no user was specified.

For example, this URL does not work:
Quote
But this one does:
Quote

I had to set the "rpcuser" in my node's bitcoin.conf file in order to get PHP to play nicely.
vess (OP)
Full Member
***
Offline Offline

Activity: 141
Merit: 100



View Profile WWW
August 03, 2010, 08:31:15 PM
 #10

I encountered this as well: there's no documentation as to what auth string should be accepted in the case of no user.

It's probably best to require rpcuser and rpcpassword in future versions is my two cents. This is generally what's expected from an HTTP Auth anyway.

I'm the CEO of CoinLab (www.coinlab.com) and the Executive Director of the Bitcoin Foundation, I will identify if I'm speaking for myself or one of the organizations when I post from this account.
satoshi
Founder
Sr. Member
*
qt
Offline Offline

Activity: 364
Merit: 6723


View Profile
August 03, 2010, 09:26:26 PM
Last edit: August 04, 2010, 12:19:17 AM by satoshi
 #11

bitcoin requires the Content-Length header, but several JSON-RPC libraries do not provide it.  When the Content-Length header is absent, bitcoin returns 500 Internal Server Error.
Can you be more specific about which JSON libraries don't provide Content-Length ?  It'd be nice to document that.
I guess we should try to support the case where there's no Content-Length parameter.  I don't want to rip and replace streams though, even if it has to read one character at a time.

Edit: That is, assuming there actually are any libraries that don't support Content-Length.
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2216


Chief Scientist


View Profile WWW
August 03, 2010, 10:52:10 PM
Last edit: August 03, 2010, 11:17:29 PM by gavinandresen
 #12

The two JSON RPC libs available at CPAN (Perl), and a compliant C lib that I wrote locally to verify the behavior.
Perl's LWP module definitely sets the Content-Length header.  I would've been surprised if it didn't, since it is required by HTTP 1.0 and the HTTP 1.1 spec says clients 'SHOULD' set it.

After some struggle, I got the first JSON::RPC library at CPAN to work:
Code:
use JSON::RPC::Client;
use Data::Dumper;
 
my $client = new JSON::RPC::Client;

$client->ua->credentials(
   'localhost:8332', 'jsonrpc', 'my rpcusername' => 'my rpcpassword'   # Replace with real user/pass
    );
my @foo = $client->ua->credentials('localhost:8332', 'jsonrpc');
print "@foo\n";

my $uri = 'http://localhost:8332/';
my $obj = {
    method  => 'getinfo',
    params  => [],
 };
 
my $res = $client->call( $uri, $obj );
 
if($res){
    if ($res->is_error) {
        print "Error : ", $res->error_message;
    }
    else {
        print Dumper($res->result);
    }
}
else {
    print $client->status_line;
}
The struggle was setting the realm to 'jsonrpc' (it is fussy about that).  I'll document that on the wiki.


How often do you get the chance to work on a potentially world-changing project?
jgarzik
Legendary
*
qt
Offline Offline

Activity: 1596
Merit: 1091


View Profile
August 04, 2010, 04:24:16 AM
 #13

Content-Length is definitely not sent on Fedora 12 and Fedora 13, will investigate further.  Will check RHEL/CentOS too.

Jeff Garzik, Bloq CEO, former bitcoin core dev team; opinions are my own.
Visit bloq.com / metronome.io
Donations / tip jar: 1BrufViLKnSWtuWGkryPsKsxonV2NQ7Tcj
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!