Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

WWW:::Mechanize and credentials

by Sifmole (Chaplain)
on Mar 09, 2005 at 15:18 UTC ( #437928=perlquestion: print w/ replies, xml ) Need Help??
Sifmole has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, I am trying to set up a short Mecahnize script to request a page, but for some reason the passing of credentials isn't doing what I expect. The reponse I get seems to be implying that the credentials are not getting passed.
Here is the code I am using:
#!/usr/local/bin/perl -w use WWW::Mechanize; use strict; my $mech = WWW::Mechanize->new( ); my $url = 'https://mybox/bin/engine.cgi'; my $base = 'https://mybox'; my $username = 'foo'; my $password = 'foo'; $mech->credentials($base, $username, $password); $mech->get( $url ); use Data::Dumper; print Dumper($mech);
Result:
HTML><HEAD> <TITLE>401 Authorization Required</TITLE> </HEAD><BODY> <H1>Authorization Required</H1> 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> <HR> <ADDRESS>Apache/1.3.28 Server at [ip address] Port 443</ADDRESS> </BODY></HTML>
Two items:
1) I know the URL is correct
2) I know the username and password combination is correct.
Any help you call can provide is appreciated.

Edit: fixed code so that is matches what I really had ( both netlocs matching ).

Comment on WWW:::Mechanize and credentials
Select or Download Code
Re: WWW:::Mechanize and credentials
by saintmike (Vicar) on Mar 09, 2005 at 15:29 UTC
    The credentials method expects four parameters:

        $ua->credentials( $netloc, $realm, $uname, $pass )

    Also, your $netloc doesn't match the netloc in the URL. WWW::Mechanize (aka the underlying LWP::UserAgent) will only send the credentials if both netlocs are matching.

Re: WWW:::Mechanize and credentials
by brian_d_foy (Abbot) on Mar 09, 2005 at 15:35 UTC

    The credentials method comes straight from LWP::UserAgent (since WWW::Mechanize is a subclass). It takes four arguments: the base, realm, user, and password. You don't have the realm in there, so the user name becomes the realm, and so on.

    "Normal" browsers do this in two steps: they try to fetch the resource and get a 401 response that contains the challenge (which has the realm information). They ask the user for the name and password, which they then use to request the resource again. They save that information for future accesses to the same realm. The realm helps the user-agent keep things straight on the user side, and the server doesn't really care about it otherwise. Sometimes you'll need to pull the auth method from the challenge, but usually it's just "Basic".

    If you want to do it in one step (so you don't get the 401 at all), you stick the "Authorization" header in the initial request. It makes no difference to the server that you don't know the realm name. With HTTP::Request you just add the header to the request. In WWW::Mechanize, you can use the add_header() method. The HTTP specification (RFC 2616) explains the format of the Authorization header.

    --
    brian d foy <bdfoy@cpan.org>
      Nice. I've added this to the Mech Cookbook.

      xoxo,
      Andy

Re: WWW:::Mechanize and credentials
by Limbic~Region (Chancellor) on Mar 09, 2005 at 15:52 UTC
    Sifmole,
    While the other answers in this thread are good, sometimes it helps to have example code:
    #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $url= 'http://10.11.12.13/password.html'; my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->credentials( '10.11.12.13:80', 'ABCDEF', 'admin' => 'password' ); $mech->get( $url ); print $mech->content();

    Cheers - L~R

      Nice. I've added this to the Mech Cookbook.

      xoxo,
      Andy

Re: WWW:::Mechanize and credentials
by Sifmole (Chaplain) on Mar 09, 2005 at 16:09 UTC
    Thank you all for your replies, It is working now.
      please will you do help of me.i am getting same problem like as you but till now it is not resolve.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://437928]
Approved by Corion
Front-paged by holli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2014-09-21 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (165 votes), past polls