Instantly share code, notes, and snippets.

@monde /.gitignore
Last active Nov 3, 2018

What would you like to do?
Micro Gem to get an OAuth token and secret for the API allowing an external application to post blog.

Generate a OAuth token and secret for the API

This is a micro Gem to get an OAuth token and secret for the API allowing an external application to post 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
bundle install

Go to the Tumblr OAuth Applications panel, 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"

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

Click "Register" and you'll be redirected back to 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 ""
gem "generate-tumblr-oauth-token", :git => ""
gem "oauth"
#!/usr/bin/env ruby
require 'generate-tumblr-oauth-token.rb'
puts "running generate" do |s| = 'generate-tumblr-oauth-token'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY = 'Mike Mondragon' = ''
s.summary = 'Generate a OAuth token and secret for the API'
s.description = 'This is a micro Gem to get an OAuth token and secret for the API allowing an external application to post 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"
require 'pp'
require 'readline'
require 'rubygems'
require 'bundler/setup'
require 'oauth'
readme = File.join(File.dirname(__FILE__), '')
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 =
{ :site => '',
: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}
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.

This comment has been minimized.

kyletcarlson commented Jun 6, 2013

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?


This comment has been minimized.


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.


This comment has been minimized.

pmoran commented Aug 14, 2013

Following!topic/tumblr-api/foJZZdSKO2s, updating to use

consumer.get_request_token(:exclude_callback => true)

got this working for me. Thanks for the gist.


This comment has been minimized.

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