Skip to content

Instantly share code, notes, and snippets.

@monde
Last active February 19, 2020 19:02
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save monde/4577106 to your computer and use it in GitHub Desktop.
Save monde/4577106 to your computer and use it in GitHub Desktop.
Micro Gem to get an OAuth token and secret for the Tumblr.com API allowing an external application to post Tumblr.com blog.
Gemfile.lock

Generate a OAuth token and secret for the Tumblr.com API

This is a micro Gem to get an OAuth token and secret for the Tumblr.com API allowing an external application to post Tumblr.com blog.

Make a directory, cd into it, download the microgem's Gemfile and run bundle install

mkdir /path/to/some/new/dir
cd /path/to/some/new/dir
wget https://gist.github.com/raw/4577106/6bc9befedcd5238ce9f2ee562cace666dece460c/Gemfile
bundle install

Go to the Tumblr OAuth Applications panel, http://www.tumblr.com/oauth/apps and log in if prompted.

Click the "+ Register application" button to grant access to your application and fill in the following to register a new application. Unless you know otherwise, use this URL for the "Default callback URL" http://sasquatch.github.com/tumblr.html

  • Application name
  • Application website
  • Application description
  • Administrative contact email
  • Default callback URL

Click "Register" and you'll be redirected back to http://www.tumblr.com/oauth/apps Make note of your new "OAuth Consumer Key" and "Show secret key / Secret Key" for the application you've just registered.

Now generate an OAuth token and secret that your application will use to post to your tumblr in the following manner.

bundle exec generate-token
source "http://rubygems.org"
gem "generate-tumblr-oauth-token", :git => "https://gist.github.com/4577106.git"
gem "oauth"
#!/usr/bin/env ruby
require 'generate-tumblr-oauth-token.rb'
puts "running generate"
Gem::Specification.new do |s|
s.name = 'generate-tumblr-oauth-token'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.author = 'Mike Mondragon'
s.email = 'mikemondragon@gmail.com'
s.summary = 'Generate a OAuth token and secret for the Tumblr.com API'
s.description = 'This is a micro Gem to get an OAuth token and secret for the Tumblr.com API allowing an external application to post Tumblr.com blog.'
s.files = ['generate-tumblr-oauth-token.rb', 'generate-token']
s.require_path = '.'
s.bindir = '.'
s.executables << 'generate-token'
s.post_install_message = "\nto generate a your Tumblr OAuth token run the following command:\n\nbundle exec generate-token"
end
require 'pp'
require 'readline'
require 'rubygems'
require 'bundler/setup'
require 'oauth'
readme = File.join(File.dirname(__FILE__), 'README.md')
lines = open(readme).readlines[14..32]
puts lines.join
oauth_consumer_key = Readline.readline("enter your OAuth Consumer Key: ", true).strip
oauth_secret_key = Readline.readline(" enter your Secret Key: ", true).strip
consumer =
OAuth::Consumer.new(
oauth_consumer_key,
oauth_secret_key,
{ :site => 'http://www.tumblr.com',
:request_token_path => '/oauth/request_token',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token',
:http_method => :get
}
)
request_token = consumer.get_request_token(:exclude_callback => true)
puts <<WORDS
Open the following authorization url in a web browser to fetch the oauth
verification key. Select the "Allow" option if you would like to "Grant this
application read and write access to your Tumblr account?". After you do so
you will be redirected to the URL that was previously saved as the 'Default
callback URL'. You are looking for a URL parameter named 'oauth_verifier'.
Your authorization URL:\n\n#{request_token.authorize_url}
WORDS
verifier = Readline.readline("enter your oauth_verifier: ", true).strip
access_token = request_token.get_access_token(:oauth_verifier => verifier)
puts <<WORDS
Save these OAuth values for the application that you have authorized to
interact with your Tumblr account via their API.
OAuth Consumer Key: #{oauth_consumer_key}
OAuth Consumer Secret: #{oauth_secret_key}
OAuth Token: #{access_token.token}
OAuth Token Secret: #{access_token.secret}
Bye, bye.
WORDS
@SabretWoW
Copy link

I've tried multiple times to run this script, but I keep getting an Error 400: Bad Request (Unauthorized) message when it runs. Any thoughts?

@monde
Copy link
Author

monde commented Jul 18, 2013

my bad @kyletcarlson, just saw your question. Are you seeing this?

oauth-0.4.7/lib/oauth/consumer.rb:216:in `token_request': 401 Unauthorized (OAuth::Unauthorized)

I just tried it out locally with a bogus consumer key and secret, so I'm not sure if the script is sensitive to bad credentials or the oauth endpoints have changed at Tumblr, etc.

@pmoran
Copy link

pmoran commented Aug 14, 2013

Following https://groups.google.com/forum/#!topic/tumblr-api/foJZZdSKO2s, updating to use

consumer.get_request_token(:exclude_callback => true)

got this working for me. Thanks for the gist.

@Joseph-N
Copy link

Joseph-N commented Jun 2, 2014

+1 thanks alot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment