Google Answers Logo
View Question
 
Q: perl script to read secure directory html using authorization_basic ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: perl script to read secure directory html using authorization_basic
Category: Computers > Programming
Asked by: compuplus-ga
List Price: $25.00
Posted: 19 Apr 2005 16:50 PDT
Expires: 19 May 2005 16:50 PDT
Question ID: 511576
The following is a perl code I'm trying to write, to get html page
from a secure directory using the user and password. I get an error
"Authorization Required" ... I have the right user and password (it's
my own server) Any clue???

#!/usr/bin/perl
use LWP::UserAgent;
$ua = new LWP::UserAgent;
my $req = HTTP::Headers->new;

$req = HTTP::Request->new(GET => 'http://www.domain.com/dir');
$req->authorization_basic("user", "password");
print "Content-type: text/html\n\n";
print $ua->request($req)->as_string;
Answer  
Subject: Re: perl script to read secure directory html using authorization_basic
Answered By: webadept-ga on 19 Apr 2005 23:01 PDT
Rated:5 out of 5 stars
 
Hi, 

From your code example, here are the offending lines. 

$req = HTTP::Request->new(GET => 'http://www.domain.com/dir');
$req->authorization_basic("user", "password");

first add the / to the end of the string here. "dir" is not what we
are after, we are after the directory in dir

$req = HTTP::Request->new(GET => 'http://www.domain.com/dir/');

take the double quotes off and replace them with single quotes here.
$req->authorization_basic('user', 'password');

A better choice is to send variables 

my $UName = 'user';
my $Pass = 'password';
$req->authorization_basic($UName, $Pass);

Something that is not required, but when I have the odd problem with
this setup I do because it works, is to set the cookie attribute as
well. Again, normally this is not required, but for some reason, on
some setups, it works when you do and doesn't when you don't. But from
your code example given above, those are your problems right now.

If you need further help with this, just let me know with the
clarification request and I'll be happy to help you out.

I'm sure you have these links but just in case you don't

lwpcook
http://cpan.uwinnipeg.ca/htdocs/libwww-perl/lwpcook.html

LWP::UserAgent
http://cpan.uwinnipeg.ca/htdocs/libwww-perl/LWP/UserAgent.html


webadept-ga

Request for Answer Clarification by compuplus-ga on 20 Apr 2005 14:12 PDT
I tried all these thing already. They don't work. I tried them again
today. I read the links you sent me before already but it still doesnt
seem to work. Did you ever do it, or do you have a tested script that
works?

Clarification of Answer by webadept-ga on 20 Apr 2005 14:29 PDT
Hi, 

Yes I have and yes I do :-) 

Let's go over the Apache setup. Did you set this up with an .htaccess
file? or through the httpd.conf file? I am assuming you can log in by
going to the page directly. If you can post the setup for the
directory here we can rule that out, and I'll set up a test area for
you in the mean time on another server so we can test it there. Take
me a bit to get that setup as I don't have access at the moment but
I'll do it sometime tonight or early in the morning so we can get you
up and working there.

So post your setup here for the directory and I'll get my part done. 

webadept-ga

Clarification of Answer by webadept-ga on 20 Apr 2005 14:32 PDT
I'm sure I don't have to tell you this, but you don't have to send IP
addresses, URL names, user names or passwords... just make sure the
username and password (for right now) are lower case and all one word.

webadept-ga

Request for Answer Clarification by compuplus-ga on 20 Apr 2005 14:49 PDT
I created a test directory http://www.tzintz.com/test/ user and password is test

Clarification of Answer by webadept-ga on 20 Apr 2005 19:51 PDT
Okay, so something else is definitly not working on your system here

http://www.webadept.net/cgi-bin/test.cgi

Because that works, and I used the code I've already sent you, exactly
the same. Test and see that you can see the output.

So, can you describe 'what' it is doing that is not working? First
off, if you are using it as a cgi, try runing the perl script from the
command line, and see if that works. If it does, then it is probably
your cgi file permissions that are screwy. Make sure the CGI is set at
755.

Next, check your apache log files. Clear them first, and then run the
perl script, and see what it says afterwards.

let me know and I'll help you get this working ...

webadept-ga

Clarification of Answer by webadept-ga on 20 Apr 2005 19:53 PDT
Oh.. I didn't add the cookie in the code.. here's the code that the
test.cgi is running.

---
#!/usr/bin/perl
use LWP::UserAgent;
$ua = new LWP::UserAgent;
my $req = HTTP::Headers->new;

$req = HTTP::Request->new(GET => 'http://www.tzintz.com/test/');
$req->authorization_basic("test", "test");
print "Content-type: text/html\n\n";
print $ua->request($req)->as_string;

----

Also, check the little things like the she-bang is the first thing on
the first line and that the perl it is pointing to is the right one
and all that other stuff. I know it sounds silly and obvious but you
have no idea how much hair I've lost over a missing ";" .. :-)

webadept-ga

Clarification of Answer by webadept-ga on 21 Apr 2005 10:18 PDT
Hi again, 

Just letting you know that I'm going to be out for most of the
afternoon, but I'll check as I can during the day to see if you have
posted something (like your apache error_log) or have any other
questions regarding this. I will check and keep the email up this
evening as well.

You are appear to be running Apache on Linux or Unix, so this isn't an
issue, but there is an issue with IIS and LWP when doing this on the
local machine. Just to check your setup, if you are using the URL
localhost, try using the full URL and see if that helps you out at
all.

webadept-ga

Request for Answer Clarification by compuplus-ga on 21 Apr 2005 12:03 PDT
Thank you so much, the test is working but the main directory I need
is not. Can you check if the following is working by you.
http://www.tzintz.com/stats/ users, passwords: test

Clarification of Answer by webadept-ga on 21 Apr 2005 14:49 PDT
No problem and it is working fine from my laptop and my server as a CGI program. 

http://www.webadept.net/cgi-bin/test2.cgi

---- Code ----

#!/usr/bin/perl
use LWP::UserAgent;
$ua = new LWP::UserAgent;
my $req = HTTP::Headers->new;

$req = HTTP::Request->new(GET => 'http://www.tzintz.com/stats/');
$req->authorization_basic('test', 'test');
print "Content-type: text/html\n\n";
print $ua->request($req)->as_string;

---- End Code ----

You also might check to see that there are no empty blank lines after
the last line of your code, and that your script is formated in Unix
format, not windows format. That last one is a real rub, ... drove me
mad more than once.

webadept-ga
compuplus-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
Thank you so much. Now it's working fine.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy