karzilla: a green fist above the word SMASH! (Default)
[staff profile] karzilla

AS ORIGINALLY WRITTEN THIS DOES NOT WORK WITH THE CURRENT API. SEE UPDATES BELOW.

If you follow [twitter.com profile] kareila you have already seen this, but there's a [twitter.com profile] dw_alerts account that posts errors from Dreamwidth's Nagios log, and it was broken until I fixed it a couple of days ago. It wasn't hard. Here's what I did:

Request API keys from Twitter

1] Go to https://dev.twitter.com/apps and sign in with the account you are using with your script (in my case, dw_alerts).

2] Click "Create a new application".

3] Give your application a name - I used "DW Nagios Gateway" - and a description (in my case, also "DW Nagios Gateway"). The only other required field is website - use whatever you feel is appropriate here. Leave "Callback URL" blank, agree to the terms, prove your sentience, and submit the form.

4] Now you should be on the Details page for your new application. Before you do anything else, click the Settings tab and scroll down to the choices for "Application Type." Change the selection from "Read only" to "Read and Write," because you want your script to post messages.

5] Go back to the Details tab and click the button to create access tokens.

6] Now from the OAuth tab, you should be able to copy the four keys you need. In your Perl script, it would look something like this:

my %oauth_info = (
    consumer_key => 'aaaa',
    consumer_secret => 'bbbb',
    access_token => 'cccc',
    access_token_secret => 'dddd',
);

(Those values should be long gibberish looking strings, which I am simplifying here.)

This is what you use instead of a username and password to authenticate using the new API.

Update your code to use the new API

Your mileage may vary, but for my purposes, I was able to use Net::Twitter::Lite instead of the more full-featured Net::Twitter. You also need to install and explicitly use Net::OAuth and Mozilla::CA.

use Net::Twitter::Lite;
use Net::OAuth;
use Mozilla::CA;

Here are the three lines of code I use to post $msg to Twitter:

my $tw = Net::Twitter::Lite->new( %oauth_info, ssl => 1 ) or die;
eval { $tw->update( $msg ) };
warn "Twitter error: $@\n" if $@;

That's it! Let me know if you have any problems.

UPDATE: This broke again a week later, when Twitter completely turned off the 1.0 API. But again, CPAN comes to the rescue and the fix is easy: instead of Net::Twitter::Lite, use Net::Twitter::Lite::WithAPIv1_1. Everything else still works as before.

UPDATE 1/2014: Twitter now requires SSL, so I've added "ssl => 1" and "use Mozilla::CA" to the code above to reflect this.

Date: 2014-01-18 03:07 pm (UTC)
alierak: (Default)
From: [personal profile] alierak
Also, as of 1/14/2014, Twitter is requiring SSL on all API calls. I found that I needed to add ", ssl => 1" to the new() parameters. Net::Twitter::Lite version 0.12005 gives a warning if you don't specify the ssl option but still defaults it to 0 for backwards compatibility on non-Twitter services.

Also had to figure out where to get a bundle of CA certificates in order for Net::Twitter::Lite, and thus LWP::UserAgent, and thus openssl, to find and use them to verify twitter.com certificates. I'm not really having any luck with this part yet, and just getting "500: Can't verify SSL peers without knowning which Certificate Authorities to trust".

Date: 2014-01-18 03:20 pm (UTC)
alierak: (Default)
From: [personal profile] alierak
Ok, got it. I left out one level of hashref in this passing ssl args within useragent args within constructor args business. Now I have this working:
my $tw = Net::Twitter::Lite::WithAPIv1_1->new( %oauth_info, ssl => 1,
  useragent_args => { ssl_opts => { SSL_ca_path => "/System/Library/OpenSSL/certs" } } );


(Got the cert files from /usr/share/ca-certificates/mozilla/ on a linux box)

Date: 2014-01-18 03:37 pm (UTC)
alierak: (Default)
From: [personal profile] alierak
Read some LWP::UserAgent source to figure out that installing Mozilla::CA from CPAN is simpler. ETA: if installed, LWP::UserAgent will use it by default and not need to be given useragent args in order to find it, so the constructor call is just:
my $tw = Net::Twitter::Lite::WithAPIv1_1->new( %oauth_info, ssl => 1 );
Edited Date: 2014-01-18 03:40 pm (UTC)

Date: 2014-01-23 02:39 pm (UTC)
kareila: (Default)
From: [personal profile] kareila
Thanks for doing the legwork on this. I updated my clean_cli script pretty easily. I'll look at Nagios later today.

Date: 2014-01-23 06:27 pm (UTC)
kareila: "PERL!" (perl)
From: [personal profile] kareila
I made the necessary updates to the copy of the Nagios script in my home directory, but the machine needs Mozilla::CA installed, and I don't know if you need to install that via apt-get or via cpan. I also don't remember if the script was running out of that directory or if we copied it elsewhere.

Profile

karzilla: a green fist above the word SMASH! (Default)
Karzilla, Destroyer of Bugs

November 2022

S M T W T F S
  12345
678910 1112
13141516171819
20212223242526
27282930   

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 15th, 2026 10:37 am
Powered by Dreamwidth Studios