Twitter LED Scale

Hi I have put together a script for the twitter timeline in which it works, furthermore, I do not know how to authorize my tweeter api key. My LED sign just says "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;

      

+3


source to share


1 answer


First: use strict;

and use warnings;

. Even if you are the most reliable programmer, this should be your first port of call if you are having trouble. (And everybody makes typos.)

Second: $json_text

- hash ref, not ref array. You probably want to use values

or similar.

Third: This Bad Authentication Data

is a twit api bug, not a code bug. You need to authorize it with oAuth and you don't do anything 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.

eg. you cannot do what you are doing without authenticating. I think you need this: https://dev.twitter.com/oauth/overview/application-owner-access-tokens

Specifically, the "simple response" generates an account authentication token and sends it to your request.

Once you've done it on this Twitter webpage: https://dev.twitter.com/oauth/tools/signature-generator/

allows you to generate a time-limited example command that you could use to get the timeline. But you will most likely need to create authentication in your script in order to do what you are trying to do. (user_timeline.json is a restricted API). (There is a "test oAuth" button on the application web page).

Reading the docs related to generating authentication tokens leads me to speculate that setting 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 at https://apps.twitter.com/ you need:

  • create application
  • create a token (from the app page).

(Under the keys and access token on the application page).

This will give you 4 things you need to tweet:

  • user key
  • user secret
  • access token
  • access token secret

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

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

#!/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 most recent tweets, which I think was what you wanted?

+4


source







All Articles