Skip to content

Instantly share code, notes, and snippets.

@Snarp
Last active April 2, 2016 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snarp/4cb73066f8c5269199f092a444891a62 to your computer and use it in GitHub Desktop.
Save Snarp/4cb73066f8c5269199f092a444891a62 to your computer and use it in GitHub Desktop.
Example of how to use OAuth to access the Tumblr API in Sinatra.
gem 'sinatra', '~> 1.4.7'
require 'sinatra'
gem 'oauth', '~> 0.5.1'
require 'oauth'
gem 'tumblr_client', '~> 0.8.5'
require 'tumblr_client'
# This is an example of how to use OAuth to access the Tumblr API in Sinatra.
# Based on dcesteri's Twitter example:
# https://github.com/dcestari/sinatra-twitter-oauth-sample
# Thanks, dcesteri.
# STEP 1: Register your app with Tumblr at
# https://www.tumblr.com/oauth/register
# The data you input isn't all that important, but it's easiest to use
# http://localhost:4567/oauth/callback
# as the default callback url.
# STEP 2: Fill in these two variables with the consumer key and secret you got
# in Step 1.
CONSUMER_KEY = "Your consumer key goes here."
CONSUMER_SECRET = "Your consumer secret goes here."
CALLBACK_URL = "http://localhost:4567/oauth/callback"
ENDPOINT = "https://www.tumblr.com"
configure do
enable :sessions
set :session_secret, 'secret'
end
# STEP 3: Run this script.
# STEP 4: In your browser, while logged in to a Tumblr account, go to
# http://localhost:4567/oauth/request_token
# and click the link.
get '/oauth/request_token' do
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => ENDPOINT)
request_token = consumer.get_request_token(:oauth_callback => CALLBACK_URL)
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
puts "request: token=#{session[:request_token]}, token_secret=#{session[:request_token_secret]}"
"<a href='#{request_token.authorize_url}'>#{request_token.authorize_url}</a>"
end
# STEP 5: If you didn't get forwarded there automatically, go to
# http://localhost:4567/oauth/callback
# If everything worked, you've completed the OAuth dance successfully, and
# you'll see some output from the API.
get '/oauth/callback' do
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => ENDPOINT)
puts "CALLBACK: request: token=#{session[:request_token]}, token_secret=#{session[:request_token_secret]}"
request_token = OAuth::RequestToken.new(consumer, session[:request_token], session[:request_token_secret])
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
Tumblr.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = access_token.token
config.oauth_token_secret = access_token.secret
end
# Finally, we have our Tumblr::Client object!
client = Tumblr::Client.new
"We have our Tumblr::Client object! Let's fetch some data with it.<br /><br />
#{client.info}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment