I suspect that the problem with your call to credentials is that both its first and second arguments are wrong. The first argument should end with a colon followed by a port number.

Here's the relevant code from LWP::UserAgent:

sub credentials { my($self, $netloc, $realm, $uid, $pass) = @_; @{ $self->{'basic_authentication'}{lc($netloc)}{$realm} } = ($uid, $pass); } sub get_basic_credentials { my($self, $realm, $uri, $proxy) = @_; return if $proxy; my $host_port = lc($uri->host_port); if (exists $self->{'basic_authentication'}{$host_port}{$realm}) { return @{ $self->{'basic_authentication'}{$host_port}{$realm} +}; } return (undef, undef); }
get_basic_credentials gets called during the authentication step, and it accessses the information that was originally initialized via credentials.

Therefore, on the basis of all this, here is another solution, again based on overriding get_basic_credentials, but this time for the purpose of getting exactly the parameters you need to feed credentials to properly configure the user agent object.

package Nonce; use base 'WWW::Mechanize'; sub get_basic_credentials { my ( $self, $realm, $uri, $proxy ) = @_; my $netloc = lc $uri->host_port; local $| = 1; print "<$netloc>\n<$realm>\n"; return undef; } package main; my $M = Nonce->new(); $M->get('http://anywhere.com/mytest?rm=test', @args); # etc.
The above will fail, BUT it will print out the values of $netloc and $realm that you need to give as arguments to credential (in addition to the username and password) to configure the user agent for interacting with this server.

Or if you don't find the idea of using the Perl debugger too distasteful, you can run your scraping script under the Perl debugger and set a breakpoint in LWP::UserAgent::get_basic_credentials, and once inside this function you can examine the values of the desired variables to your heart's content.

Once you determine the correct first and second args for credentials, you can write your script proper:

use WWW::Mechanize; my $M = WWW::Mechanize->new(); $M->credentials( 'host:port', 'realm', 'user', 'password' ); # etc.

the lowliest monk


In reply to Re: Supplying authentification credentials with WWW::Mechanize by tlm
in thread Supplying authentification credentials with WWW::Mechanize by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.