401 with REST API in Perl

727 views
Skip to first unread message

Michael Dubakov

unread,
Sep 8, 2011, 10:42:13 AM9/8/11
to DevTargetProcess
use LWP::UserAgent; 
use HTTP::Request::Common; 
use Data::Dumper; 
 
 
my $url = 'http://targetprocess/TargeProcess2/api/v1/UserStories/'; 
 
# Set up the ntlm client and then the base64 encoded ntlm handshake
message 
my $ua = LWP::UserAgent->new(keep_alive=>1); 
$ua->credentials('http://targetprocess:80', '', 'admin', 'admin'); 
 
$request = GET $url; 
$request->authorization_basic('admin', 'admin'); 
$request->header('Accept', 'text/html'); 
 
print "--Performing request now...-----------\n"; 
print Dumper($request); 
$response = $ua->request($request); 
print "--Done with request-------------------\n"; 
 
if ($response->is_success) {print "It worked!->" . $response->code .
"\n"} 
else {print "It didn't work!->" . $response->code . "\n"} 
 
#print $response->content; 
~


we are getting a 401 back

Andray

unread,
Sep 8, 2011, 11:32:27 AM9/8/11
to DevTargetProcess
Try to remove "http://" from servername in $ua->credentials(...) call,
so it should be following:
$ua->credentials('targetprocess:80', '', 'admin', 'admin');

If this doesn't help, provide us please a console output of script
running.
Thanks!

nuggit

unread,
Sep 8, 2011, 11:50:45 AM9/8/11
to DevTargetProcess
Yeah I tried that.

Here is the dump of the request and response.....I do see that NTLM
seems to not be a supported authentication scheme. Is this the
problem?

--Performing request now...-----------
$VAR1 = bless( {
'_content' => '',
'_uri' => bless( do{\(my $o = 'http://targetprocess/
TargetProcess2/api/v1/UserStories/')}, 'URI::http' ),
'_headers' => bless( {
'authorization' => 'Basic
YWRtaW46YWRtaW4='
}, 'HTTP::Headers' ),
'_method' => 'GET'
}, 'HTTP::Request' );
--Done with request-------------------
It didn't work!->401
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>You are not authorized to view this page</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html;
charset=Windows-1252">
<STYLE type="text/css">
BODY { font: 8pt/12pt verdana }
H1 { font: 13pt/15pt verdana }
H2 { font: 8pt/12pt verdana }
A:link { color: red }
A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>

<h1>You are not authorized to view this page</h1>
You do not have permission to view this directory or page using the
credentials that you supplied.
<hr>
<p>Please try the following:</p>
<ul>
<li>Contact the Web site administrator if you believe you should be
able to view this directory or page.</li>
<li>Click the <a href="javascript:location.reload()">Refresh</a>
button to try again with different credentials.</li>
</ul>
<h2>HTTP Error 401.1 - Unauthorized: Access is denied due to invalid
credentials.<br>Internet Information Services (IIS)</h2>
<hr>
<p>Technical Information (for support personnel)</p>
<ul>
<li>Go to <a href="http://go.microsoft.com/fwlink/?
linkid=8180">Microsoft Product Support Services</a> and perform a
title search for the words <b>HTTP</b> and <b>401</b>.</li>
<li>Open <b>IIS Help</b>, which is accessible in IIS Manager
(inetmgr),
and search for topics titled <b>Authentication</b>, <b>Access
Control</b>, and <b>About Custom Error Messages</b>.</li>
</ul>

</TD></TR></TABLE></BODY></HTML>
',
'_rc' => 401,
'_headers' => bless( {
'x-powered-by' => 'ASP.NET',
'client-response-num' => 1,
'date' => 'Thu, 08 Sep 2011
15:49:19 GMT',
'client-peer' =>
'192.168.1.142:80',
'content-length' => '1539',
'client-warning' =>
'Unsupported authentication scheme \'ntlm\'',
'client-date' => 'Thu, 08 Sep
2011 15:49:19 GMT',
'content-type' => 'text/html',
'www-authenticate' => [

'Negotiate',
'NTLM'
],
'server' => 'Microsoft-IIS/
6.0',
'title' => 'You are not
authorized to view this page'
}, 'HTTP::Headers' ),
'_msg' => 'Unauthorized',
'_request' => bless( {
'_content' => '',
'_uri' => bless( do{\(my $o =
'http://targetprocess/TargetProcess2/api/v1/UserStories/')},
'URI::http' ),
'_headers' => bless( {
'user-
agent' => 'libwww-perl/5.837',

'authorization' => 'Basic YWRtaW46YWRtaW4='
},
'HTTP::Headers' ),
'_method' => 'GET',
'_uri_canonical' => $VAR1-
>{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );

AnthonyBY

unread,
Sep 8, 2011, 3:13:49 PM9/8/11
to DevTargetProcess
Hello Nuggit,
We have reproduced issue with NTLM authentication. You can try to
enable ‘Anonymous authentication’ as temporary workaround. We continue
our investigation. We will give you a feedback tomorrow. Sorry for the
inconvenience.

Andray

unread,
Sep 9, 2011, 8:07:39 AM9/9/11
to DevTargetProcess
Nuggit,
here are necessary steps you need to perform to get data via rest from
pearl using ntlm authentication:

Configuration:
1) Ensure that windows authentication is enabled for TargetProcess
virtual folder.
2) Ensure that mapping between your domain user and target process
user exists. You can set this mapping in TargetProcess Edit Person
page (People tab) in "Active Directory User Logon Name" text box.

Pearl script:

use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
# i imported this module just to make sure that it exists. See my
explanation below.
use Authen::NTLM;

my $url = 'http://localhost/targetprocess/api/v1/UserStories/';
my $ua = LWP::UserAgent->new(keep_alive=>1);
#trace request and response
$ua->add_handler("request_send", sub { shift->dump; return });
$ua->add_handler("response_done", sub { shift->dump; return });
$ua->credentials("localhost:80", '', "YourDomain\\YourUserName",
"YourPassword");
$request = GET $url;
$request->header('Accept', 'text/html');
print "--Performing request now...-----------\n";
print Dumper($request);
$response = $ua->request($request);
print "--Done with request-------------------\n";
if ($response->is_success) {print "It worked!->" . $response->code .
"\n"}
else {print "It didn't work!->" . $response->code . "\n"}


I ran this script on windows 7 on Strawberry Perl 5.12.3.0.
I faced following issue, there was no Authen::NTLM module in lib
folder. As a result LWP::Authen::Ntlm didnt work because of failed
importing of Authen::NTLM.
I downloaded and added Authen::NTLM module and got working script.


On Sep 8, 6:50 pm, nuggit <herbscru...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages