Need to send a tweet, grab a user’s Twitter timeline or send a direct message on Twitter with PowerShell? This post is for you.
Related: Want to apply what you’ll learn here? Check out this post on automating following Twitter users!
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 allows you to perform a few actions with Twitter. 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 PowerShell Twitter 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.
Table of Contents
Creating the Twitter Application
- Go to Twitter’s application page and click Sign In (if you’re not already)
- Click on Create a new App.
3. Fill in your App’s details. What you put here really doesn’t matter. I’ve included a screenshot of what mine looks like.
4. Scroll down a little bit, confirm your life away and create your application.
5. 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.
6. 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
7. Click Details a few times now and your details page should eventually show Read and Write as it’s Access Level.
Creating an Access Token
You’re going to need a few things to insert into the PowerShell Twitter 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.
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. Now, let’s put them into the
Get-OAuthAuthorization function so you can begin tweeting away!
Authenticating with the Access Token
Once you’ve downloaded MyTwitter from GitHub, 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'.
More from Adam The Automator & Friends
Get this interactive comic book to learn how Veeam and AWS can help you fight ransomware, data sprawl, rising cloud costs, unforeseen data loss and make you a hero!
ATA is known for its high-quality written tutorials in the form of blog posts. Support ATA with ATA Guidebook PDF eBooks available offline and with no ads!
Check out all of the ATA recommended resources!