Skip to content

Instantly share code, notes, and snippets.

@Snarp
Created October 1, 2016 01:58
Show Gist options
  • Save Snarp/01758a5ee4dcbe065bd7a84165ff92ab to your computer and use it in GitHub Desktop.
Save Snarp/01758a5ee4dcbe065bd7a84165ff92ab to your computer and use it in GitHub Desktop.
Example of how to use Omniauth to access the Tumblr API in Sinatra.
gem 'sinatra', '~> 1.4.7'
require 'sinatra'
gem 'omniauth-tumblr', '~> 1.2'
require 'omniauth-tumblr'
gem 'tumblr_client', '~> 0.8.5'
require 'tumblr_client'
require 'yaml'
# omniauth-tumblr + tumblr_client + sinatra Example
# ------
#
# 2016-09-30
#
# This is an example of how to use Omniauth to access Tumblr API V2 via
# Sinatra. Once you've gone through the process once in Sinatra, you can save
# the OAuth token and token secret to use elsewhere.
#
# A guide to doing this without Omniauth is here:
#
# https://gist.github.com/Snarp/4cb73066f8c5269199f092a444891a62
# - STEP 1:
#
# Register your app with Tumblr at:
#
# https://www.tumblr.com/oauth/register
#
# For your the callback URL, enter:
#
# http://localhost:4567/oauth/callback
#
# When you're done, you'll be given a consumer key and secret to use below.
# - STEP 2:
#
# Fill in these two variables with your consumer key and secret:
#
CONSUMER_KEY = "YOUR CONSUMER KEY GOES HERE"
CONSUMER_SECRET = "YOUR CONSUMER SECRET GOES HERE"
# - STEP 3:
#
# Fill in a random string of characters here:
#
configure do
enable :sessions
set :session_secret, "RANDOM STRING GOES HERE"
end
use OmniAuth::Strategies::Tumblr, CONSUMER_KEY, CONSUMER_SECRET
# - STEP 4:
#
# Run this script.
# - STEP 5:
#
# In your browser, while logged in to a Tumblr account, go to
#
# http://localhost:4567/
#
# and give Tumblr permission to connect to this app.
#
get '/' do
redirect to "/auth/tumblr"
end
get '/auth/tumblr/callback' do
if !!env['omniauth.auth']
session[:authenticated] = true
else
halt(401, 'Not Authorized')
end
auth = request.env['omniauth.auth']
session[:user_id] = auth['uid']
session[:oauth_token] = auth['credentials']['token']
session[:oauth_token_secret] = auth['credentials']['secret']
# ^ Saving credentials to a cookie.
redirect to "/we_did_it"
end
get '/auth/failure' do
"#{params[:message]}"
end
# - STEP 6:
#
# Here, we use the credentials we saved to a cookie above to configure and
# a Tumblr::Client object, which is what we use to actually access the API.
#
get "/we_did_it" do
Tumblr.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = session[:oauth_token]
config.oauth_token_secret = session[:oauth_token_secret]
end
client = Tumblr::Client.new
# Some output to be sure that it's working:
#
"<p>A YAML serialization of your client object, including your OAuth info:</p>
<p><pre>#{YAML::dump(client)}</pre></p>
<p>If you're not working on a web-based app like this one, save a copy of the information above now - you'll need at least your token and token secret each time you initialize a new client object.</p>
<p>To make sure the client's working, below is a YAML dump of a hash representing the first post on your dashboard. (You need to be following at least one blog for this to show anything.)</p>
<p><pre>#{YAML::dump(client.dashboard(limit: 1))}</pre></p>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment