in reply to lwp (mechanize) authentication

I don't think there is a hook or callback that will make LWP::UserAgent ask your code to supply credentials. All credentials must be supplied before making the request.

If your request fails even though you believe you have supplied the appropriate username and password, have you checked that the $netloc and $realm parameters are correct, and match what the server sends and what LWP::UserAgent expect? The LWP::UserAgent documentation says

The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:
$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret" +);