Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^6: WWW::Mechnize redirect handling

by nikster (Novice)
on Nov 23, 2019 at 14:55 UTC ( [id://11109107]=note: print w/replies, xml ) Need Help??


in reply to Re^5: WWW::Mechnize redirect handling
in thread [Solved] WWW::Mechnize redirect handling

I've inserted your code into my script.

But the response is exactly the same as the one I've posted before.

If I try against facebook, like you did, I get all the redirect headers.

Must be something about the site itself, unfortunately there is no one to ask.

Thank's very much, though!

Replies are listed 'Best First'.
Re^7: WWW::Mechnize redirect handling
by nikster (Novice) on Dec 02, 2019 at 19:08 UTC

    For the sake of closing this thread, I have solved this.

    In the end it was not overly complex but there was just more to it than initially known.

    tl;dr: the trick was not to follow the redirect after receiving the ticket (this invalidated the ticket already).

    Thanks for your help bliako.

    #!/usr/bin/env perl ###Modules use WWW::Mechanize; use HTTP::Cookies; use HTTP::CookieJar::LWP (); use IO::Socket::SSL qw(); use Data::Dumper; use JSON; ###Variables & Declarations my $creds = "$ENV{'HOME'}/.credentials"; my $uri ="https://xxx.employer.xxx/app/login?service=https://xxx.emplo +yer.xxx/app/service"; my $cookie_jar = HTTP::Cookies->new(); my ($username,$password) = get_credentials($creds); my $fields = { username => $username, password => $password, }; my $m = WWW::Mechanize->new( cookie_jar => $cookie_jar, autocheck => 0 +, ssl_opts => { SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE, +verify_hostname => 0 }, env_proxy => 1, keep_alive => 1, timeout => 1 +20, agent => 'Windows IE 6' ); $m->max_redirect(0); ############## Log in and get Ticket ################## my $content = $m->post($uri); $m->submit_form( form_number => 1, fields => $fields, button => 'submit' ); my $location = $m->response()->header('Location'); my $ticket_id = (split /ticket=/, $location)[1]; ############## /Log in and get Ticket ################## ############## Create Session and get authorization id ############### +### $m->add_header('Content-Type' => 'text/plain'); $m->add_header('Accept' => ['text/plain', 'application/json']); #$m->delete_header('Referer'); my $session_url = "https://xxx.employer.xxx:<port>/session"; my $contentp = $m->post($session_url, 'Content' => "$ticket_id"); my $resp = $contentp->decoded_content()."\n\n"; my $decoded_json = decode_json( $resp ); my $id = $decoded_json->{id}; ############## /create session and get authorization id############### +### ############## do authorized stuff ######################### $m->add_header('Authorization' => $id); $m->post("whatever"); from here on I'm doing stuff inside the session...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109107]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found