Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2016/07/27

Net::Twitter Cookbook: How to Tweet



The first line between Twitter Use and Twitter Obsession is TweetDeck. That's when the update-on-demand single-thread of the web page gives way to multiple constantly-updated streams of the stream-of-consciousness ramblings of the Internet.

That's the first line.

The second line between Twitter use and Twitter obsession is when you want to automate the work. If you're an R person, that's twitteR. If you work in Python, that's tweepy.

And, if you're like me, and you normally use Perl, we're talking Net::Twitter.

What follows is the simplest possible Net::Twitter program.

  1. #!/usr/bin/env perl  
  2. use feature qw{ say } ;  
  3. use strict ;  
  4. use warnings ;  
  5. use Net::Twitter ;  
  6.   
  7. # the consumer key and secret identify you as a service.   
  8. # you register your service at https://apps.twitter.com/  
  9. # and receive the key and secret  
  10.   
  11. # you really don't want to have these written into your script  
  12.   
  13. my $consumer_key    = 'ckckckckckckckckckckck' ;  
  14. my $consumer_secret = 'cscscscscscscscscscscscscscscscscscscscs' ;  
  15.   
  16. my $twit = Net::Twitter->new(  
  17.     traits          => [qw/API::RESTv1_1/],  
  18.     consumer_key    => $consumer_key,  
  19.     consumer_secret => $consumer_secret,  
  20.     ssl             => 1,  
  21.     ) ;  
  22.   
  23. # the access token and secret identify you as a user.  
  24. # the registration process takes place below.  
  25. # the first time you run this program, you will not be authorized,  
  26. # and the program will give you a URL to open in a browser where  
  27. # you are already logged into twitter.  
  28.   
  29. # you really don't want to have these written into your script  
  30.   
  31. my $access_token = '1111111111-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ;  
  32. my $access_token_secret = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' ;  
  33.   
  34. $twit->access_token($access_token) ;  
  35. $twit->access_token_secret($access_token_secret) ;  
  36.   
  37. # everything interesting will occur inside this if statement  
  38. if ( $twit->authorized ) {  
  39.     if ( $twit->update('Hello World!') ) {  
  40.         say 'It worked!' ;  
  41.         }  
  42.     else {  
  43.         say 'Fail' ;  
  44.         }  
  45.     }  
  46. else {  
  47.     # You have no auth token  
  48.     # go to the auth website.  
  49.     # they'll ask you if you wanna do this, then give you a PIN  
  50.     # input it here and it'll register you.  
  51.     # then save your token vals.  
  52.   
  53.     say "Authorize this app at "$twit->get_authorization_url,  
  54.         ' and enter the PIN#' ;  
  55.     my $pin = <stdin> ;    # wait for input  
  56.     chomp $pin ;  
  57.     my ( $access_token$access_token_secret$user_id$screen_name ) =  
  58.         $twit->request_access_token( verifier => $pin ) ;  
  59.   
  60.     say 'The following lines need to be copied and pasted above' ;  
  61.     say $access_token ;  
  62.     say $access_token_secret ;  
  63.     }  

Again, this is as simple as we can reasonably do, without pulling the keys into a separate file, which I, again, strongly recommend you do. (I personally use YAML as the way I store and restore data such as access tokens and consumer keys. I will demonstrate that in a later post.)

No comments:

Post a Comment