in reply to Better Solution To WWW::Mechanize Basic Authentication

See the get_basic_credentials method in the callback section of LWP::UserAgent. Mech inherits from LWP::UA.

Update: credentials(), afaict, takes 4 parameters, not two. Perhaps that is the root of the reason it isn't working. Reached for my favorite hammer when a screwdriver was already provided.

Update 2: Per almut in Re^4: Better Solution To WWW::Mechanize Basic Authentication, I stand corrected. Two parameter version is quite correct.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: Better Solution To WWW::Mechanize Basic Authentication
by Limbic~Region (Chancellor) on Oct 19, 2009 at 15:51 UTC
    MidLifeXis,
    Mech inherits from LWP::UA.

    Yes, I know. That's how I was able to solve the problem - using the inherited default_header() method. I still don't see how your suggestion helps.

    My understanding after RTFM is that this is just a way to get user name and password not set it. It just uses previously stored information from the credentials() method - which I indicate didn't work for me.

    Can you please show me an example of how you think this might help me?

    Cheers - L~R

      Note: vastly simplified.

      package MyUA; use base 'LWP::UserAgent'; { my $user; my $pass; sub set_creds { $user = shift; $pass = shift; } sub get_basic_credentials { ($user, $pass) } }

      get_basic_credentials() is called by the GET/POST/FOO methods when challenged (perhaps more often, you may need to verify this). I use it to read passwords from a config file, prompt from a terminal w/o echoing, and other times that I need to access restricted sites.

      Update: credentials(), afaict, takes 4 parameters, not two. Perhaps that is the root of the reason it isn't working. Reached for my favorite hammer when a screwdriver was already provided.

      Update 2: Per almut in Re^4: Better Solution To WWW::Mechanize Basic Authentication, I stand corrected. Two parameter version is quite correct.

      --MidLifeXis

        credentials(), afaict, takes 4 parameters, not two.

        Later versions also do support two parameters. From the Changes file:

        1.20 Sat Aug 19 09:09:08 EDT 2006 [ENHANCEMENTS] * Added new two-argument form of credentials() method. $mech->credentials($username, $password); That provides simpler visiting of password-protected resources in the vast majority of cases and still allows the other cases to be supported. (Peter Scott)
        MidLifeXis,
        Yes, get_basic_credentials() is called but, unless overidden, just pulls the information from credentials(). As it turns out, WWW::Mechanize does overide the method but just marginally.

        None of that changes why it is isn't working. In other words, calling $mech->credentials() before calling $mech->submit() should "just work" but it doesn't.

        Are you suggesting subclassing WWW::Mechanize to hard code the values on a per-instance basis? That seems further from my objective (work within the confines of WWW::Mechanize).

        Cheers - L~R