09/23/14 UPDATE: I’ve created a Github repo for this module. I realize it needs a lot of work if it’s going to be a full-fledged Twitter client so please help me make it great!
I don’t know about you but I’m on Twitter all the time. It’s a great way to connect with like-minded people. You can use Twitter simply through Twitter.com or through the slew of Twitter clients out there. Just like any other major web service, Twitter provides an API. In a nutshell, an API is the way most companies allow others to interact with their service. In Twitter’s case, APIs are the reason you’re probably not tweeting from twitter.com all the time and instead you’ve got 3 Twitter clients on your phone, a Twitter client on your Mac and one on your PC. These clients are all making API calls to Twitter’s servers to interact with any number of Twitter services. In the example I’m about to talk about, Twitter’s API allowed me to send tweets and DMs from my account with Powershell.
If you go looking on the web you’ll find a lot of references to using Twitter with Powershell. I didn’t set out to create my own module but after trying out the various scripts out there and failing miserably at getting any to work I decided to just create my own. Twitter’s API has went through a few revisions. At the time of this writing, they are up to v1.1 which includes a much more secure feature called OAuth. OAuth drastically changed the way you interact with Twitter’s APIs because you now had to authenticate and authorize yourself ahead of time. I won’t get into the specifics but needless to say, it requires a TON more code than it did.
The majority of the links I found were for previous API versions which simply wouldn’t work at all. Some looked similar but simply would give me “Not Authorized” or “Failed to authenticate” errors when I attempted to use them. The closest example I could find was Ian Chiver’s blog post entitled Tweet from Powershell Using OAuth but it was built for API v1 and not 1.1. I’ve discovered that OAuth is extremely picky and I just couldn’t get anything to work so I decided to take the leap and learn about Oauth and Twitter’s APIs.
As I alluded to earlier, I’ve created a Powershell module called MyTwitter that (as of this writing) allows you to send tweets and DMs to other Twitter users from your own Twitter account. Maybe one of these days I’ll tack on some other functionality since I’ve got OAuth handled… Unlike my other scripts/modules you can’t just simply import and go off tweeting. You first need to perform a few prerequisite steps to get your Twitter account prepped to use Twitter’s APIs.
Before I get to the module, if you’d like to tweet or send DMs from your Twitter account in Powershell you’re going to have to create a Twitter application first. I promise it’s not as daunting as it seems. I created my first application this weekend and it was really easy. Here’s what you need to do prior to importing this module to get tweeting ASAP.
- Go to Twitter’s application page and click Sign In (if you’re not already)
- Click on Create a new App.
- Fill in your App’s details. What you put here really doesn’t matter. I’ve included a screenshot of what mine looks like.
- Scroll down a little bit, confirm your life away and create your application.
- You’ll then come to a screen like this. Whenever your app is built, it will be read-only which means you’ll only be able to read tweets and other things but can’t actually send a tweet or a DM. We need to change the app to either Read and Write or Read, Write and Access Direct Messages.
- When I was testing I thought the app needed Read, Write and Access Direct Messages but I was wrong. Your app only needs Read and Write permission. Click Update Settings
- Click Details a few times now and your details page should eventually show Read and Write as it’s Access Level.
- You’re going to need a few things to insert into the module now that the app is created. The first set of attributes you need are the API Key and API Secret. You can find these by clicking on the API Keys section. Be sure to copy these out. You’ll need them later.
- Next, you need to create an Access Token. This is a token that your module will use to prove who you say you are. To do this, scroll down a little bit on the API Keys page and click on Create my Access Token.
- Wait a little bit and this screen will come up. You will need the access token and access token secret for the module.
That wasn’t so bad, was it? After you’ve successfully created your first Twitter application you should have come away with 4 key pieces of information; the API key, the API secret, an Access token and an Access token secret. You’ve got these, right? Good! Now, let’s put them into the Get-OAuthAuthorization function so you can begin tweeting away!
I’ve uploaded the module to the Technet Script Repository so go ahead and download it. Once you’ve downloaded it, go to the Get-OAuthAuthorization function and paste in those 4 pieces of information you got from your Twitter application.
Once you do that, you’re good to go! If you made it to the end of this post kudos to you! Now, the only thing I ask for you to do for me in return for my hard work here is to tweet me as your first tweet with this module. Once you’ve got it imported, just do a
Send-Tweet -Message '@adbertram Thanks for the Powershell Twitter module'