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

Hi Monks,

I wrote a perl script using WWW:Mechanize as I am trying to communicate with my link. when I execute the script, it is giving me "Unauthorized" error at line

$mech->get($url);
then I read some nodes with similar issue and saw that adding below code solved their issue:
my $mech = WWW::Mechanize->new(); my @args = (Authorization => "Basic " .MIME::Base64::encode('USER:PASS ++')); $mech->get('http://mylink/', @args); $mech->get('http://mylink/'); $mech->credentials($username,$password);

but even by adding the authetication code, I am still seeing "Unauthorized" error.

Please Advise....

Thanks in Advance

Replies are listed 'Best First'.
Re: New with WWW:Mechanize and Credentials issue
by ww (Archbishop) on May 12, 2009 at 20:18 UTC

    Did you assign values to $username and password?

    No intent to insult, but the information and code you've posted above are insufficient to offer help that's much better than the 'stab in the dark' above.

    Pls post a minimal version of your code that demonstrates the problem.

Re: New with WWW:Mechanize and Credentials issue
by almut (Canon) on May 13, 2009 at 00:20 UTC

    Have you tried the most simple example from the Cookbook?

    $mech->credentials( $username => $password ); $mech->get( 'http://mylink/' );

    Note that the credentials are set up before the get().

Re: New with WWW:Mechanize and Credentials issue
by Anonymous Monk on May 12, 2009 at 20:31 UTC
    Sorry..I was thinking,I posted it... Here is the Code:
    #!c:\\perl\\bin use LWP::Debug qw(+); use strict; use WWW::Mechanize; use HTTP::Cookies; use MIME::Base64; my $outfile = "out.txt"; #my $url = "http://mylink/blah/"; my $username = "username"; my $password = "password"; my $mech = WWW::Mechanize->new(); my @args = (Authorization => "Basic " .MIME::Base64::encode('USER:PASS +')); $mech->get('http://mylink/blah/', @args); $mech->credentials($username,$password); $mech->click(); my $output_page = $mech->content(); open(OUTFILE, ">$outfile"); print OUTFILE "$output_page"; close(OUTFILE);
Re: New with WWW:Mechanize and Credentials issue
by imrags (Monk) on May 13, 2009 at 06:25 UTC
    Try
    $agent->get($URL); $agent->credentials("$host:80","$URL",$user,$pass);
    Try with port 80 Or any specific port if mentioned(after $host)
    $host can be something like http://yahoo.com or similar.
    Raghu