Authentication problems with 1.5 api via perl.

201 views
Skip to first unread message

Jeff

unread,
Oct 21, 2010, 2:06:16 PM10/21/10
to reviewboard
I'm trying to write an svn pre-commit hook against a totally new
installation of reviewboard 1.5 (upgraded from the previous version,
but never really used).

As far as I can tell from the documentation, the json login page isn't
used anymore, and I just use basic HTTP authentication.

My perl code looks like this:

#!/usr/bin/perl
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
#$ua->credentials("$rbhost:80","Web API",'user' => 'password');

my $req = HTTP::Request->new(GET => "http://reviewboard.local.com/
rboard/api/review-requests/93/last-update/");
$req->authorization_basic('user', 'password');

my $res = $ua->request($req);
print $res->as_string;


I've tried both the useragent credential and the request-
>authorization_basic.

The result I get back is:


HTTP/1.1 401 UNAUTHORIZED
Cache-Control: max-age=0
Connection: close
Date: Thu, 21 Oct 2010 17:53:43 GMT
ETag: "3818aa0b0928af747aebc006814783fe"
Server: Apache/2.2.3 (CentOS)
Vary: Cookie,Accept-Language
WWW-Authenticate: Basic realm="Web API"
Content-Language: en-us
Content-Length: 70
Content-Type: application/json
Expires: Thu, 21 Oct 2010 17:53:43 GMT
Last-Modified: Thu, 21 Oct 2010 17:53:43 GMT
Client-Date: Thu, 21 Oct 2010 17:53:43 GMT
Client-Peer: 10.20.30.183:80
Client-Response-Num: 1

{"stat": "fail", "err": {"msg": "You are not logged in", "code": 103}}



If I try and hit the same page in an web browser after authenticating,
I get a 500 error and an email post at the end of this message.

What am I doing wrong? Is there any example perl code that hit's the
new API? I assume the existing stuff that I'm finding on the net
won't work anymore.


Traceback (most recent call last):

File "/usr/local/reviewboard/python/Django-1.2.1-py2.7.egg/django/
core/handlers/base.py", line 100, in get_response
response = callback(request, *callback_args, **callback_kwargs)

File "/usr/local/reviewboard/python/Django-1.2.1-py2.7.egg/django/
views/decorators/cache.py", line 70, in _wrapped_view_func
add_never_cache_headers(response)

File "/usr/local/reviewboard/python/Django-1.2.1-py2.7.egg/django/
utils/cache.py", line 116, in add_never_cache_headers
patch_response_headers(response, cache_timeout=-1)

File "/usr/local/reviewboard/python/Django-1.2.1-py2.7.egg/django/
utils/cache.py", line 105, in patch_response_headers
response['ETag'] = '"%s"' %
md5_constructor(response.content).hexdigest()

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 273, in _get_content
content = adapter.encode(self.api_data, request=self.request)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 119, in encode
self.__encode(o, *args, **kwargs)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 136, in __encode
self.__encode(value, *args, **kwargs)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 136, in __encode
self.__encode(value, *args, **kwargs)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 164, in __encode
return self.__encode(result, *args, **kwargs)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 136, in __encode
self.__encode(value, *args, **kwargs)

File "/usr/local/reviewboard/python/Djblets-0.6.4-py2.7.egg/djblets/
webapi/core.py", line 162, in __encode
raise TypeError("%r is not XML serializable" % (o,))

TypeError: 2L is not XML serializable


<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'csrftoken': '3c25a25c4c6ced636686afac7a25a4f1',
'rbsessionid': '2aeedaf117ffddcef6c72c0d5a2a407b'},
META:{'DOCUMENT_ROOT': '/usr/local/reviewboard/web/htdocs',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/
xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'HTTP_ACCEPT_ENCODING': 'gzip,deflate',
'HTTP_ACCEPT_LANGUAGE': 'en-us,en;q=0.5',
'HTTP_CONNECTION': 'keep-alive',
'HTTP_COOKIE': 'rbsessionid=2aeedaf117ffddcef6c72c0d5a2a407b;
csrftoken=3c25a25c4c6ced636686afac7a25a4f1',
'HTTP_HOST': 'reviewboard',
'HTTP_KEEP_ALIVE': '115',
'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10',
'PATH': '/sbin:/usr/sbin:/bin:/usr/bin',
'PATH_INFO': u'/rboard/api/review-requests/93/last-update/',
'PATH_TRANSLATED': '/usr/local/reviewboard/web/htdocs/
reviewboard.fcgi/rboard/api/review-requests/93/last-update/',
'QUERY_STRING': '',
'REMOTE_ADDR': '10.20.68.52',
'REMOTE_PORT': '3620',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/rboard/api/review-requests/93/last-update/',
'SCRIPT_FILENAME': '/usr/local/reviewboard/web/htdocs/
reviewboard.fcgi',
'SCRIPT_NAME': u'',
'SCRIPT_URI': 'http://reviewboard/rboard/api/review-requests/93/last-
update/',
'SCRIPT_URL': '/rboard/api/review-requests/93/last-update/',
'SERVER_ADDR': '10.20.30.183',
'SERVER_ADMIN': 'root@localhost',
'SERVER_NAME': 'reviewboard',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SIGNATURE': '<address>Apache/2.2.3 (CentOS) Server at
reviewboard Port 80</address>\n',
'SERVER_SOFTWARE': 'Apache/2.2.3 (CentOS)',
'wsgi.errors': <flup.server.fcgi_base.OutputStream object at
0x197b4c50>,
'wsgi.input': <flup.server.fcgi_base.InputStream object at
0x197b4910>,
'wsgi.multiprocess': False,
'wsgi.multithread': True,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>

Christian Hammond

unread,
Oct 27, 2010, 2:22:25 AM10/27/10
to revie...@googlegroups.com
Hi Jeff,

The 500 error you're hitting is fixed in the latest Djblets release. You can upgrade by doing: easy_install -U Djblets

It's been ages since I last attempted anything with Perl's HTTP code, so I can't really say what's going wrong there, except that the typical process for Basic HTTP Auth is that you'll make a request, see a 401 Unauthorized, and then you're supposed to send a request with the proper credentials. I'd hope that their HTTP support would handle this for you, based on what you're doing, but perhaps not.

I'd be interested in seeing if your code works with other sites using Basic HTTP Auth. If so, it may be a bug on our end.

Christian

--
Christian Hammond - chi...@chipx86.com
Review Board - http://www.reviewboard.org
VMware, Inc. - http://www.vmware.com



--
Want to help the Review Board project? Donate today at http://www.reviewboard.org/donate/
Happy user? Let us know at http://www.reviewboard.org/users/
-~----------~----~----~----~------~----~------~--~---
To unsubscribe from this group, send email to reviewboard...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/reviewboard?hl=en

Gav

unread,
Aug 10, 2012, 5:26:12 AM8/10/12
to revie...@googlegroups.com, jeffrey...@gmail.com
Hi Jeff, did you ever get this resolved? I am facing a similar issue.

Christian, I am loving Reviewboard. Thank You!!!

Cheers,
Gav

cmuser

unread,
Feb 25, 2013, 8:25:13 AM2/25/13
to revie...@googlegroups.com, jeffrey...@gmail.com
i have tried to built like this,but still not working

#!/usr/local/bin/perl

use LWP::UserAgent;
use MIME::Base64;

$url="http://codereview.xyz.com/api/review-requests/";
$newurl="http://codereview.xyz.com/r/new/";
$user="admin";
$pwd="admin";

$text="$user:$pwd";
$text = encode_base64($text);
#print "Encoded text: $text\n";
%param =(
        "repository_id" =>"cvsrepo",
        "repository_path" =>":pserver:user:pwd\@svn:/opt/cvsroot",
        "username" => "admin",
        "password" => "admin",
        "submit-as" => "username",
        );
   my $ua = LWP::UserAgent->new;
    $ua->timeout(TIMEOUT);
    $ua->protocols_allowed(['http', 'https']);
    $ua->default_header('Basic' => $text);
    $ua->credentials($newurl,$user,$pwd);

        $resp = $ua->get($url);
        print($resp->status_line(), "\n");

print("Add handler:\n");
        $ua->add_handler( response_header => sub { print "HANDLER\n"; }, %param);
        print $ua->handlers('response_header', $resp)."\n";
push @{ $ua->requests_redirectable }, 'POST';
print $ua->show_progress."\n";
my $response = $ua->post($newurl,%param);

 if ($response->is_success) {
     print $response->decoded_content;
 }
 else {
     die $response->status_line;
 }

Gavin Main

unread,
Feb 26, 2013, 3:38:54 AM2/26/13
to revie...@googlegroups.com, jeffrey...@gmail.com
After a bit of fiddling and research I found that the LWP and HTTP::Cookies modules worked for me. Have a look at the documentation on CPAN

http://search.cpan.org/~gaas/HTTP-Cookies-6.01/lib/HTTP/Cookies.pm

This link was also very helful:
http://lwp.interglacial.com/ch11_01.htm


Basically I wanted my post-commit hook to close out an associated review on Reviewboard. So I wrote two subroutines to do that. The first generated the cookie:
- create the cookie file
- issue the get request with HTTP::Request
- authenticate with authorization_basic
- extract the cookie
- return hash

Then I passed the cookie hash to another subroutine which issued my PUT request with the information inside the cookie hash.

Cheers,
Gavin

cmuser

unread,
Feb 26, 2013, 3:45:43 AM2/26/13
to revie...@googlegroups.com, jeffrey...@gmail.com
Thanks. I  will try & follow it.
What i did is , tried writing the script(mentioned above) to post a review request which is failing of xyz reason. So thought of using HTTP::Recorder to get the script of posting a review request. But again facing issues in that.

curl command which worked once -
curl   -k -H "Basic YWRtaW46YWRtaW4=" -X POST  -d "{\"repository_id\": "cvsrepo",\"repository_path\":":pserver:kapila:kapila123@svn:/opt/cvsroot",\"username\":"kapila",\"password\":"kapila123",\"submit-as\":"kapila"}"  "http://codereview.xyz.com/r/new

Would be great if you can share your script to give quick & better understanding.

Gavin Main

unread,
Feb 26, 2013, 3:57:12 AM2/26/13
to revie...@googlegroups.com, jeffrey...@gmail.com
I didn't realise you were trying to post a review. Does post-review (bundled with RBTools) not help you? If not, why?

Christian Hammond

unread,
Feb 26, 2013, 4:04:05 AM2/26/13
to revie...@googlegroups.com, jeffrey...@gmail.com
The curl command is pretty wrong.

You're still not passing an Authorization header. -H by itself doesn't turn the value into one. You're also submitting JSON data, not form data. The server won't be able to recognize any of that.

Christian

-- 
Christian Hammond - chi...@chipx86.com
Review Board - http://www.reviewboard.org
VMware, Inc. - http://www.vmware.com

--
Want to help the Review Board project? Donate today at http://www.reviewboard.org/donate/
Happy user? Let us know at http://www.reviewboard.org/users/
-~----------~----~----~----~------~----~------~--~---
To unsubscribe from this group, send email to reviewboard...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/reviewboard?hl=en
---
You received this message because you are subscribed to the Google Groups "reviewboard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reviewboard...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

cmuser

unread,
Feb 26, 2013, 5:23:11 AM2/26/13
to revie...@googlegroups.com, jeffrey...@gmail.com, chi...@chipx86.com
Sorry for disturbing you all asking such stupid question. But i am badly stuck. :(
This is what i tried now, still no gain

curl   -k -H "Authorization: Basic YWRtaW46YWRtaW4=" -X POST  -d "repository_id=cvsrepo&username=kapila&password=kapila123&submit-as=kapila" http://codereview.xyz.com/r/new/

wondering why this command worked yesterday once

 curl   -k -H "Authorization: Basic YWRtaW46YWRtaW4=" -X POST  -d "{\"repository_id\": "cvsrepo",\"repository_path\":":pserver:kapilap:kapila123@svn:/opt/cvsroot",\"username\":"kapila",\"password\":"kapila123",\"submit-as\":"kapila"}"  "http://codereview.xyz.com/r/new/"

@gavin, I am trying to integrate Bugzilla with Reviewboard so as a review request gets created whenever a new bug filled.

Christian Hammond

unread,
Feb 26, 2013, 5:29:12 AM2/26/13
to revie...@googlegroups.com
Go back and thoroughly read and understand the API documentation. Read the post-review source code. You're taking wild stabs in the dark with incorrect fields and URLs. That URL you're posting to is not an API URL.

Also, keep all this to *one* thread. There's posts from you about this all over the place now, and that's not okay.

Christian

-- 
Christian Hammond - chi...@chipx86.com
Review Board - http://www.reviewboard.org
VMware, Inc. - http://www.vmware.com

kapila narang

unread,
Feb 26, 2013, 5:31:38 AM2/26/13
to revie...@googlegroups.com
Thanks for giving me the lead.. i will try that out.
Apologize for miscommunication.

cmuser

unread,
Feb 28, 2013, 7:41:15 AM2/28/13
to revie...@googlegroups.com

  I am now able to post the review as draft from below script, but unable to set the parameters. Can u pls suggest where i m going wrong?
Getting error-
Can't locate object method "previous" via package "submit_as" (perhaps you forgot to load "submit_as"?) at /usr/lib/perl5/site_perl/5.8.8/HTTP/Response.pm line 107.


#!/usr/local/bin/perl

use strict;
use warnings;
$| = 1;

use LWP::UserAgent;
use HTTP::Request::Common qw(GET POST);
use Data::Dumper;
use JSON;

my $url="http://codereview.com/api/review-requests/";
#my $user="admin";
#my $pwd="admin";
my $user="kapila";
my $pwd="kapila123";
my %param =(
        repository_id =>"cvsrepo",
        submit_as => "kapila"

        );

   my $ua = LWP::UserAgent->new;
   my $req = HTTP::Request->new(POST => "$url");
        $req ->authorization_basic($user,$pwd);
   #my $resp= $ua->request($req);
   my $resp= $ua->request($req,%param);

die "Error: ", $resp->status_line
unless $resp->is_success;
my $result = from_json($resp->content);
my $id=$result->{review_request}->{id};
print "new request id-$id\n";

my $publishurl="http://codereview.com/api/review-requests/";
To unsubscribe from this group, send email to reviewboard+unsubscribe@googlegroups.com

For more options, visit this group at http://groups.google.com/group/reviewboard?hl=en
---
You received this message because you are subscribed to the Google Groups "reviewboard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reviewboard+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
Want to help the Review Board project? Donate today at http://www.reviewboard.org/donate/
Happy user? Let us know at http://www.reviewboard.org/users/
-~----------~----~----~----~------~----~------~--~---
To unsubscribe from this group, send email to reviewboard+unsubscribe@googlegroups.com

For more options, visit this group at http://groups.google.com/group/reviewboard?hl=en
---
You received this message because you are subscribed to the Google Groups "reviewboard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reviewboard+unsubscribe@googlegroups.com.

Jay Lawrence

unread,
Feb 28, 2013, 10:00:37 AM2/28/13
to revie...@googlegroups.com, jeffrey...@gmail.com
I don't know this interface, but I suspect that you need to pass a reference to your params. instead of 
   my $resp= $ua->request($req,%param);
try
   my $resp= $ua->request($req,\%param);

Note the extra '\'.

jay

cmuser

unread,
Mar 1, 2013, 12:35:02 AM3/1/13
to revie...@googlegroups.com, jeffrey...@gmail.com
hash reference give me this error

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)")
Reply all
Reply to author
Forward
0 new messages