推特 LED 时间线 [英] Twitter LED Timeline

查看:51
本文介绍了推特 LED 时间线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我为 twitter 时间线编写了一个脚本,除了我不知道如何授权我的 twitter api 密钥之外,我的 LED 标志只是说错误的身份验证数据"

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"

这是我的代码

#!/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;

推荐答案

首先:use strict;use warnings;.即使您是有史以来最棒的程序员,如果您遇到问题,这也应该是您的第一站.(而且每个人都会打错字).

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).

其次:$json_text 是一个哈希引用,而不是一个数组引用.您可能想要使用 values 或类似的.

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

第三:Bad Authentication Data 是 Twitter api 错误,而不是代码错误.您需要使用 oAuth 对其进行授权,而您根本没有进行 twitter 身份验证.来自:https://dev.twitter.com/overview/api/response-codes

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 - 通常与 HTTP 代码 400 的 1.1 响应一起发送.该方法需要身份验证,但未提供或完全无效.

例如如果不进行身份验证,您将无法做您正在做的事情.我想你需要的是这个:https://dev.twitter.com/oauth/overview/application-owner-访问令牌

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.

在 Twitter 上完成这个这个网页后:https://dev.twitter.com/oauth/tools/signature-generator/

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

允许您生成可用于获取时间线的(限时)示例命令.但是您很可能需要在脚本中构建身份验证才能执行您想要执行的操作.(user_timeline.json 是一个受限访问 API).(应用程序网页上有一个测试 oAuth"按钮).

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).

通读有关创建身份验证令牌的文档,让我觉得安装 Net::TwitterNet::Twitter::Lite 可能是要走的路.

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.

首先按照此处的说明进行操作:https://dev.twitter.com/oauth/overview/application-owner-access-tokens

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

具体来说,在 https://apps.twitter.com/ 上,您需要:

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

  • 创建应用程序
  • 生成令牌(从应用程序页面).

(在应用程序特定页面上的密钥和访问令牌"下).

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

这将为您提供与 Twitter 交谈所需的 4 件事:

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

  • 消费者密钥
  • 消费者秘密
  • 访问令牌
  • 访问令牌秘密

默认情况下,您的访问令牌将是只读的.这对于您想做的事情来说很好.

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

将它们转换为 Twitter 身份验证的过程有点复杂,并且涉及 RSA-HMAC 加密.所以就让 Net::Twitter 为你做这件事吧:(我已经删除了我的密钥,因为我还不够愚蠢,无法发布相当于我的 Twitter 密码)

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?

这篇关于推特 LED 时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆