Perl interface to Twitter API

Creating a user login and password for twiiter using Net::Twitter modu

Page 1 of 1

3 Replies - 3624 Views - Last Post: 11 January 2010 - 05:17 AM

#1 pia_18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-January 10

Perl interface to Twitter API

Post icon  Posted 07 January 2010 - 10:37 PM

Hi there,
I have been trying to create a user login and password for twitter as already mentioned using Net::Twitter module from CPAN.
But on execution of the program it neither shows any error nor any output.Well,if we type anything with print statement,it only gets printed not the exact thing which I wish to get.
The coding used is here:

#!/usr/bin/perl

use strict;
use warnings;

my $user='TwitterUsername';
my $password='TwitterPassword';

use Net::Twitter;
  my $nt = Net::Twitter-> new(
	  traits   => ['Legacy'],
	  username => $user,
	  password => $password);
	  
 my $result = $nt->update('Hello, world!');

my $statuses='';
my $high_water='';
  eval {
	  my $statuses = $nt->friends_timeline({ since_id => $high_water, count => 100 });
	  for my $status ( @{$statuses} ) {
		  print "$status->{time} <$status->{user}{screen_name}> $status->{text}\n";
	  }
  };  
if ( my $err = $@ ) {
	  die $@ unless blessed $err && $err->isa('Net::Twitter::Error');

	  warn "HTTP Response Code: ", $err->code, "\n",
		   "HTTP Message......: ", $err->message, "\n",
		   "Twitter error.....: ", $err->error, "\n";
  }



Could you tell me where I went wrong in this program or what should I add if there is anything is missing to execute it properly?


Thanks
pia

Is This A Good Question/Topic? 0
  • +

Replies To: Perl interface to Twitter API

#2 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl interface to Twitter API

Posted 08 January 2010 - 03:55 AM

View Postpia_18, on 8 Jan, 2010 - 05:37 AM, said:

But on execution of the program it neither shows any error nor any output.

What about the attempted status update? Does that appear in your Twitter timeline?

That could help with determining whether you're logging in successfully or not.

use Net::Twitter;
  my $nt = Net::Twitter-> new(
	  traits   => ['Legacy'],
	  username => $user,
	  password => $password);


Personally, I prefer to use traits => [ qw( API::REST WrapError ) ] and have never tried Legacy, but I doubt that's the cause of your problem.

	  
my $statuses='';
my $high_water='';


Not really necessary, btw. Initializing variables to 'empty' values in Perl is generally considered a waste of time, given that undef serves that purpose implicitly.

	  my $statuses = $nt->friends_timeline({ since_id => $high_water, count => 100 });


I have a vague memory of having run into problems in the past when passing a since_id of 0. Try initializing $high_water with a value of 1 instead of an empty string.

  eval {
  };
if ( my $err = $@ ) {
  }


Be aware that this construct can, in some cases, fail to properly identify exceptions. ($@ is global, so something else might have set/changed/cleared it.)
eval {
} or do {
  my $err = $@;
};


Is better, in that it is guaranteed to correctly determine whether an exception has occurred or not, although it still might get the wrong message in $@ when an exception occurs.

Look into TryCatch or Try::Tiny if you want to be sure that you always report the correct exception as well as always reporting correctly whether one occurred or not.
Was This Post Helpful? 0
  • +
  • -

#3 pia_18   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-January 10

Re: Perl interface to Twitter API

Posted 11 January 2010 - 03:00 AM

Thanks,
I have tried out with the modifications you have said,but nothing seems to work.i will now try out with Trycatch.
thanks for your help.

Pia
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl interface to Twitter API

Posted 11 January 2010 - 05:17 AM

View Postpia_18, on 11 Jan, 2010 - 10:00 AM, said:

I have tried out with the modifications you have said,but nothing seems to work.i will now try out with Trycatch.

TryCatch probably won't make a difference in this case, I just mentioned it as a point of general good practice.

If you're familiar with IRC, try hopping on #net-twitter at irc.perl.org and asking there - the Net::Twitter maintainers hang out there, so they could probably diagnose your issue pretty quickly.

If not, re-post the latest version of your script and I can poke at it a bit to see if I can get it working tomorrow or Weds. I only drop by here once a day, though, so you'll probably get help faster on IRC if you can ask there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1