Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter LED Timeline

Tags:

twitter

perl

Hello I have put together a script for a twitter timeline it works apart from i dont know how to authorize my twitter api key my led sign is just saying "Bad Authentication data"

here is my code

#!/usr/bin/perl

require LWP::UserAgent;
use JSON;

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

my $uagent = "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://api.twitter.com/', 'User-Agent' => $uagent );

my $twuser = '<twitter_name>';
my $twurl  = "http://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=$twuser";

my $response = $lwpua->get( $twurl, @header );
my $return = $response->content;

my $json      = JSON->new->allow_nonref;
my $json_text = $json->decode($return);

my @tweets = @{$json_text};

my $message;
foreach $tweet (@tweets) {
    $message .= $tweet->{text} . "\n";
}

use Device::MiniLED;
my $sign = Device::MiniLED->new( devicetype => "sign" );

$sign->addMsg(
    data   => "$message",
    effect => "scroll",
    speed  => 4
);

$sign->send( device => "/dev/ttyUSB0" );

1;
like image 719
Binney C Avatar asked May 13 '26 23:05

Binney C


1 Answers

First: use strict; and use warnings;. Even if you're the awesomest programmer ever, this should be your first port of call if you're having problems. (And everyone makes typos).

Secondly: $json_text is a hash ref, not an array ref. You probably want to use values or similar.

Thirdly: Bad Authentication Data is a twitter api error, not a code error. You need to authorize it with oAuth, and you're doing no twitter auth at all. From: https://dev.twitter.com/overview/api/response-codes

215 - Typically sent with 1.1 responses with HTTP code 400. The method requires authentication but it was not presented or was wholly invalid.

E.g. you can't do what you're doing without authenticating. I think what you need is this: https://dev.twitter.com/oauth/overview/application-owner-access-tokens

Specifically - the 'easy answer' is generate an account specific authentication token, and send that in your request.

Once you have done this this web page on Twitter: https://dev.twitter.com/oauth/tools/signature-generator/

allows you to generate a (time limited) example command that you could use to fetch your timeline. But you'll most likely need to build authentication into your script in order to do what you're trying to do. (user_timeline.json is a restricted access API). (There's a 'test oAuth' button on the app webpage).

Reading through the docs on creating authentication tokens, makes me thing that installing Net::Twitter or Net::Twitter::Lite might be the way to go.

First follow the instructions here: https://dev.twitter.com/oauth/overview/application-owner-access-tokens

Specifically, on https://apps.twitter.com/ you need to:

  • create an application
  • generate a token (from the application page).

(Under 'keys and access tokens' on the app specific page).

This will give you the 4 things you need to speak to twitter:

  • a consumer key
  • a consumer secret
  • an access token
  • an access token secret

By default, your access token will be read only. This is fine for what you want to do.

The process for turning them into Twitter auth is a bit complicated and involves RSA-HMAC encryption. So just let Net::Twitter do it for you: (I've removed my keys, because I'm not quite daft enough to post the equivalent of my Twitter password)

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

use Net::Twitter;

my $twitter = Net::Twitter->new(
    traits       => [qw/API::RESTv1_1/],
    consumer_key => 'long_string_of_stuff',
    consumer_secret =>
        'long_string_of_stuff',
    access_token => '12345-long_string_of_stuff',
    access_token_secret =>
        'long_string_of_stuff',
    ssl => 1,
);


my $tweets = $twitter->user_timeline();

print Dumper \$result;

my $message;
foreach my $tweet ( @{$tweets} ) {
    $message .= $tweet->{text} . "\n";
}

print $message;

Tested this with my account, and it prints a list of my recent tweets, which I think was what you wanted?

like image 126
Sobrique Avatar answered May 16 '26 13:05

Sobrique



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!