Created
September 21, 2010 18:44
-
-
Save javan/590264 to your computer and use it in GitHub Desktop.
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
| # Tweet::OAuth::CONSUMER_KEY and Tweet::OAuth::CONSUMER_SECRET are strings I stored locally. | |
| class OauthTwitterController < ApplicationController | |
| # Will redirect to authorize with twitter | |
| def show | |
| oauth.set_callback_url(new_oauth_twitter_url) | |
| session[:twitter_oauth_request_token] = oauth.request_token.token | |
| session[:twitter_oauth_request_token_secret] = oauth.request_token.secret | |
| redirect_to oauth.request_token.authorize_url | |
| end | |
| # Redirected back here by twitter | |
| def new | |
| begin | |
| oauth.authorize_from_request(session[:twitter_oauth_request_token], session[:twitter_oauth_request_token_secret], params[:oauth_verifier]) | |
| twitter = Twitter::Base.new(oauth) | |
| @site.update_attributes!( | |
| :twitter_username => twitter.verify_credentials.screen_name, | |
| :twitter_oauth_token => oauth.access_token.token, | |
| :twitter_oauth_token_secret => oauth.access_token.secret, | |
| :twitter_tweet_failure_count => 0, | |
| :twitter_enabled => true | |
| ) | |
| session[:twitter_oauth_request_token] = nil | |
| session[:twitter_oauth_request_token_secret] = nil | |
| flash[:notice] = "Successfully connected with Twitter." | |
| rescue => e | |
| notify_hoptoad(e) | |
| flash[:notice] = "Couldn't connect with Twitter; please try again." | |
| end | |
| redirect_to :controller => :site, :action => :edit, :anchor => :twitter | |
| end | |
| private | |
| def oauth | |
| @oauth ||= Twitter::OAuth.new(Tweet::OAuth::CONSUMER_KEY, Tweet::OAuth::CONSUMER_SECRET) | |
| 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
| def post_to_twitter!(message) | |
| begin | |
| oauth = Twitter::OAuth.new(Tweet::OAuth::CONSUMER_KEY, Tweet::OAuth::CONSUMER_SECRET) | |
| oauth.authorize_from_access(self.site.twitter_oauth_token, self.site.twitter_oauth_token_secret) | |
| twitter = Twitter::Base.new(oauth) | |
| twitter.update(message) | |
| rescue | |
| self.site.increment!(:twitter_tweet_failure_count) | |
| raise | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment