Twitter LED Timeline

前端 未结 1 1362
甜味超标
甜味超标 2021-01-06 13:54

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

1条回答
  •  情歌与酒
    2021-01-06 14:22

    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?

    0 讨论(0)
提交回复
热议问题