Skip to content

Instantly share code, notes, and snippets.

@ckdake
Created November 4, 2010 18:09
Show Gist options
  • Save ckdake/662875 to your computer and use it in GitHub Desktop.
Save ckdake/662875 to your computer and use it in GitHub Desktop.
stub for linking with and posting to twitter from rails apps
gem 'oauth'
gem 'twitter', :git => 'git://github.com/jnunemaker/twitter.git'
resource :twitter_account
match '/callback/twitter/' => "twitter_accounts#callback", :as => :twitter_callback
create_table "twitter_accounts", :force => true do |t|
t.integer "user_id"
t.boolean "active", :default => false
t.text "stream_url"
t.string "oauth_token"
t.string "oauth_token_secret"
t.string "oauth_token_verifier"
t.text "oauth_authorize_url"
end
class TwitterAccount < ActiveRecord::Base
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
OPTIONS = {:site => "http://api.twitter.com", :request_endpoint => "http://api.twitter.com"}
belongs_to :user
def authorize_url(callback_url = '')
if self.oauth_authorize_url.blank?
# Step one, generate a request URL with a request token and secret
signing_consumer = OAuth::Consumer.new(TwitterAccount::CONSUMER_KEY, TwitterAccount::CONSUMER_SECRET, TwitterAccount::OPTIONS)
request_token = signing_consumer.get_request_token(:oauth_callback => callback_url)
self.oauth_token = request_token.token
self.oauth_token_secret = request_token.secret
self.oauth_authorize_url = request_token.authorize_url
self.save!
end
self.oauth_authorize_url
end
def validate_oauth_token(oauth_verifier, callback_url = '')
begin
signing_consumer = OAuth::Consumer.new(TwitterAccount::CONSUMER_KEY, TwitterAccount::CONSUMER_SECRET, TwitterAccount::OPTIONS)
access_token = OAuth::RequestToken.new(signing_consumer, self.oauth_token, self.oauth_token_secret).
get_access_token(:oauth_verifier => oauth_verifier)
self.oauth_token = access_token.params[:oauth_token]
self.oauth_token_secret = access_token.params[:oauth_token_secret]
self.stream_url = "http://twitter.com/#{access_token.params[:screen_name]}"
self.active = true
rescue OAuth::Unauthorized
self.errors.add(:oauth_token, "Invalid OAuth token, unable to connect to twitter")
self.active = false
end
self.save!
end
def post(message)
Twitter.configure do |config|
config.consumer_key = TwitterAccount::CONSUMER_KEY
config.consumer_secret = TwitterAccount::CONSUMER_SECRET
config.oauth_token = self.oauth_token
config.oauth_token_secret = self.oauth_token_secret
end
client = Twitter::Client.new
begin
client.update(message)
return true
rescue Exception => e
self.errors.add(:oauth_token, "Unable to send to twitter: #{e.to_s}")
return false
end
end
end
class TwitterAccountsController < ApplicationController
def new
twitter_account = TwitterAccount.create(:user => current_user)
redirect_to(twitter_account.authorize_url(twitter_callback_url))
end
def callback
if params[:denied] && !params[:denied].empty?
redirect_to(deals_url, :alert => 'Unable to connect with twitter: #{parms[:denied]}')
else
twitter_account = TwitterAccount.find_by_oauth_token(params[:oauth_token])
twitter_account.validate_oauth_token(params[:oauth_verifier], twitter_callback_url)
twitter_account.save
if twitter_account.active?
redirect_to(deals_url, :notice => 'Twitter account activated!')
else
redirect_to(deals_url, :notice => "Unable to activate twitter account.")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment