Thursday, February 10, 2011

Login to foursquare via backend using PERL

This is just a simple login request to foursquare via backend using PERL. Just follow the simple steps and the complete code below.

1. Install the libraries needed.
  • LWP::UserAgent;
  • HTTP::Cookies;
2. Initialize UserAgent and Cookies.

my $lwpua = LWP::UserAgent->new;

my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ('Referer' => 'http://foursquare.com',
             'User-Agent' => $user_agent);

my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

$lwpua->cookie_jar($cookie_jar);

3. Post your username and password to https://foursquare.com/mobile/login and save the cookies.

my $strUser = "<your foursquare username>";
my $strPass = "<your foursquare password>";

# log-in to foursquare
my $response = $lwpua->post('https://foursquare.com/mobile/login',
                      ['username' => $strUser,
                       'password' => $strPass], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;

4. You can check if successful or not by accessing http://foursquare.com/mobile/ via GET and print the content.

$response = $lwpua->get("http://foursquare.com/mobile/", @header);
my $form_data = $response->content;

print "$form_data\n\ndone!";


That's it! So simple right? Please see the complete code below.

#!/usr/bin/perl

require LWP::UserAgent;

use strict;
use warnings;

use HTTP::Cookies;

my $lwpua = LWP::UserAgent->new;

my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.6) Gecko/20060728 Firefox/1.5.0.6";
my @header = ('Referer' => 'http://foursquare.com',
             'User-Agent' => $user_agent);

my $cookie_file = "cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
                file => $cookie_file,
                autosave => 1,
                ignore_discard => 1);

$lwpua->cookie_jar($cookie_jar);

my $strUser = "<your foursquare username>";
my $strPass = "<your foursquare password>";

# log-in to foursquare
my $response = $lwpua->post('https://foursquare.com/mobile/login',
                      ['username' => $strUser,
                       'password' => $strPass], @header);
$cookie_jar->extract_cookies( $response );
$cookie_jar->save;

$response = $lwpua->get("http://foursquare.com/mobile/", @header);
my $form_data = $response->content;

print "$form_data\n\ndone!";


1;


Hope you like it!! Thank you for reading. Please see other post - Login to twitter via backend using PERL

2 comments:

  1. Hi, great post! I've been trying to follow yoru example, but I'm afarid I'm making a silly mistake. When I replace (your foursquare username) with my email address, I get the followign error...


    Possible unintended interpolation of @fftrou in string at 4sqlogin.pl line 24.
    Global symbol "@fftrou" requires explicit package name at 4sqlogin.pl line 24.
    Execution of 4sqlogin.pl aborted due to compilation errors.

    Any ideas what I am doing wrong?

    ReplyDelete
  2. hey Sal, you have to escape the "@" sign of your username, for example "example\@yahoo.com" thanks!

    ReplyDelete