Plankton has asked for the wisdom of the Perl Monks concerning the following question:

Friends,

I am trying to write a script that will allow me to access a webpage that is protected with Apache Basic Authentication, but I don't know how to pass the username and password to the server from my script:

$ cat wwwPerl.pl #!/usr/bin/perl -w use strict; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $url = "http://localhost/testauth/testauth.html"; $mech->get( $url ); print $mech->content();
Here's what happpens when I run it
$ ./wwwPerl.pl <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Authorization Required</title> </head><body> <h1>Authorization Required</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.</p> </body></html>
I know I could use expect, but I don't fell like having to install Tcl right now. I know that WWW::Mechanize::Shell has an auth method. But I would want to automate things later on. Is there a way to automate WWW::Mechanize::Shell? Hmm...

Replies are listed 'Best First'.
Re: passing Apache Basic Auth. creditial with WWW::Mechanize?
by howie (Sexton) on Nov 22, 2004 at 08:57 UTC
    You can register username/password combinations for a particular server and Basic Auth Realm (the text that appears in the IE password box). Like so:
    my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->credentials( 'members.easynews.com:80', 'EasyNews', 'username' => 'password' );
    This way, WWW::Mechanize will figure out for itself when it needs to use the password, if your spidering takes you between authenticated and non-authenticated sections of a site, or between sites altogether.
Re: passing Apache Basic Auth. creditial with WWW::Mechanize?
by fglock (Vicar) on Nov 22, 2004 at 00:30 UTC

    $url = "http://user:pass@localhost/testauth/testauth.html" may work.

      Brilliant!
Re: passing Apache Basic Auth. creditial with WWW::Mechanize?
by Corion (Patriarch) on Nov 22, 2004 at 14:45 UTC

    You can always output the code created by WWW::Mechanize::Shell as a Perl script and then change the Perl script to take the parameters in the Perl - which is how I originally intended the module/program to be used :-)

    Update: I think you can even make the script directly interactive by using the WWW::Mechanize::FormFiller class ask (I think) to make your script directly/interactively ask for input.

Re: passing Apache Basic Auth. creditial with WWW::Mechanize?
by TedPride (Priest) on Nov 22, 2004 at 14:42 UTC
    fglock: Isn't that method a security breach? Your pass is provided as text to anyone who can access the URL you called.

      Other people who are simply browsing the same URL can't see it.
      However, the password is "plain" in the script and in the network packets.