How to Tweet from the Command Line in OS X

Create a Twitter Developer Account

Go to Twitter’s developer portal, click “Create an app”, and create a developer account. You will be asked to explain why you want an account; make it clear that you’re doing this to learn how APIs work for a school project, and you won’t be doing analytics on Twitter data or spamming anyone. Twitter may send you an email asking you to explain all of this again, but just reiterate what you’re doing and why, and they will likely approve you in less than an hour. While waiting for approval, you may want to start working on getting Ruby and t installed. Once approved, create a new app and give it permissions to read, write, and access DMs. [Ref #1]

 

Install Ruby

Open up iTerm and runĀ brew install ruby. Add ruby to your path by running echo 'export PATH="$HOME/.brew/lib/ruby/gems/2.6.0/bin:"' >> ~/.zshrc. Note that this is different from the path that brew tells you to add. Run source ~/.zshrc to make your new path accessible in your current terminal session.

 

Install the Twitter Command Line Client t

In iTerm, run gem install t. If you get an error, double-check that you’re using the version of gem you just installed with homebrew. Before you run t, navigate to its directory and edit the the cli.rb file: vim ~/.brew/lib/ruby/gems/2.6.0/gems/t-3.1.0/lib/t/cli.rb. Find two instances of Twitter::REST::Client::BASE_URL and replace them with Twitter::REST::Request::BASE_URL [Ref #3]. Save the file.

 

Authorize t to Use Your Twitter Account

In iTerm, runĀ t authorize. Follow the instructions to complete authorization in your preferred web browser. You can now tweet from the command line using the format t update "This is a tweet posted from the command line". [Ref #2]

 

REFERENCES:

1. Outline of tweeting from terminal, including an outdated-but-useful description of registering a developer account. https://computers.tutsplus.com/tutorials/how-to-tweet-from-terminal-on-a-mac–cms-24177
2. Official github for t. https://github.com/sferik/t
3. Workaround for bug in t that apparently hasn’t been fixed for a year. https://github.com/sferik/twitter/issues/878 (rvagg on Dec 6, 2017)