twitter oauth

Sinatra example of authenticating and uploading images to Twitter.

1
0
Ruby

twitter-oauth

Sinatra example of authenticating and uploading images to Twitter.

Have not seen an updated Twitter Auth/Post example. Here is a solution in ~100 lines in Ruby.

Our company needed the ability to post Images, so this was a nice playground for that.

Feel free to fork/PR.

Getting Started

First and foremost, you must have an set of consumer keys from Twitter. Here is the URL to start that process, https://apps.twitter.com/.

Second open a Terminal

# Install required Gems
bundle

# Setup Twitter Environment Variables
cp env.example env.local
# Open local and enter values for CONSUMER_KEY and CONSUMER_SECRET
source env.local

# Start websever
ruby ./main.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
...

Now open the browser to http://127.0.0.1:4567/login to start the 3-Legged Authorization process with Twitter.

Commands

Login

http://127.0.0.1:4567/login

Test Login

After the Twitter callback succeeds, navigate to the /test to view your OAuth credentials.

http://127.0.0.1:4567/test

Post Message

After authenticating, to post a tweet, navigate to the /post/message/ route. Here are some examples.

Post Image

After authenticating, to post a tweet with an image, navigate to the /post/image/route. Here are some examples.

Logout

To test the authentication process, you can logout and attepmpt to post again.

http://127.0.0.1:4567/logout

Resources