Posting to Twitter from a Ruby on Rails App
- User visits twitter_account/new when they want to link in Twitter
- They get redirected to the allow/deny page on Twitter that prompts the user to log in and accept your application
- Assuming they accept, they get redirected back to callback/twitter/ with an oauth_token that we use to look up the TwitterAccount (a nice feature that Facebook doesn't do!) and the oauth_verifier
- The application uses the OAuth gem to sends that verifier back to Twitter to retrieve an access_token, which can be used for all future posts to Twitter to act as the user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'oauth' | |
| gem 'twitter', :git => 'git://github.com/jnunemaker/twitter.git' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| resource :twitter_account | |
| match '/callback/twitter/' => "twitter_accounts#callback", :as => :twitter_callback |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Chris Kelly