http://www.perlmonks.org?node_id=691385

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

hi i am writting a perl code to import contacts from my gmail account. i have used mechanize, the code works fine when run from the ternminal, but when put on a web server and run using a browser i am getting an error which says bad host name,.....the apache log files show that the mechnize agaent($mech) is not able to find the form, but as i said it works perfectly on terminal...i am really stupped could anyone help me.... the code is
#!/usr/bin/perl -w use WWW::Mechanize qw(!head); use HTTP::Cookies; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print header; print start_html("email"); $user=param("user"); $pass=param("password"); ###go to login page and login. #$url = "https://www.google.com/accounts/ServiceLogin?service=mail&pas +sive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3 +Fnsr%3D1%26ui%3Dhtml%26zy%3Dl&bsv=1k96igf4806cy&ltmpl=default&ltmplca +che=2"; $url="https://www.google.com/accounts/ServiceLoginAuth?service=mail"; $mech = WWW::Mechanize->new() or die "cannot create a new agent"; $mech->cookie_jar(HTTP::Cookies->new()); $mech->get($url) or die 'cannot get'; print $mech->content(); $mech->form_number(1); die "not logging in",$mech->response->status_line unless $mech->succes +s; $mech->field(Email => 'xxxxxxx'); $mech->field(Passwd => 'yyyyyy'); $mech->submit(); #Go to the next link, now that we are logged in. $url = 'http://mail.google.com/mail/contacts/data/export?exportType=AL +L&groupToExport=&out=Google_CSV'; $mech->get($url)||die "din get"; print $mech->content();

Replies are listed 'Best First'.
Re: importing contacts using mechanize
by Erez (Priest) on Jun 11, 2008 at 06:37 UTC

    Two things,
    one, a better practice when contacting services is to use a known User Agent, such as my $mech = WWW::Mechanize->new( agent => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4"). Many services, especially Google's, tend to disregard requests from unknown agents, or from LWP::UserAgent's default "libwww-perl/5.8"

    Two, why not use a dedicated module such as Mail::Webmail::Gmail?

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

Re: importing contacts using mechanize
by Anonymous Monk on Jun 11, 2008 at 06:32 UTC
    $mech->get() or die is wrong. You'll never know if get fails (which it probably does, because of firewall or permissions on your webserver) from that statement, because get always returns a HTTP::Response object. Its time to go read the manual.
      ya, i used mech->success for the form i assumed that everything else was working fine now ill do the same for all the get functions, but can u help me out to figure out why the code works from the terminal and not from the web server..i am really not understanding this

        Most likely you are using different versions of WWW::Mechanize in the console and the webserver, or the user that the webserver is configured to run under does not have the permissions to access network resources.

Re: importing contacts using mechanize
by Anonymous Monk on Jun 11, 2008 at 06:35 UTC
      i have tried using Mail::webmail::gmail, but it doesnt seem to work, the module was written for gmail beta, and i run the example scripts nothing happens, so i thought better off sticking with mechanize, if you can help me out with webmail::gmail, as in if the .pm file has to modified or something, the gmail would be the way to go for me, thanks in advance
Re: importing contacts using mechanize
by denishowe (Acolyte) on Jan 21, 2010 at 00:03 UTC
    You might consider WWW::Contact::GoogleContactsAPI. I'm in the process of updating this to handle all the fields in the v3 Contacts API.