Created
November 2, 2010 18:22
-
-
Save ckdake/660059 to your computer and use it in GitHub Desktop.
Revisions
-
ckdake revised this gist
Nov 2, 2010 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ create_table "facebook_accounts", do |t| t.integer "user_id" t.boolean "active", :default => false t.text "stream_url" t.text "access_token" t.text "oauth_authorize_url" t.datetime "created_at" t.datetime "updated_at" end -
ckdake created this gist
Nov 2, 2010 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ gem 'rest-client' gem 'json' 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ class FacebookAccount < ActiveRecord::Base # Stubbed out! Does no (good) error checking! # Get these from facebook! FACEBOOK_CLIENT_ID = 'it' FACEBOOK_CLIENT_SECRET = 'secret' def authorize_url(callback_url = '') if self.oauth_authorize_url.blank? self.oauth_authorize_url = "https://graph.facebook.com/oauth/authorize?client_id=#{FACEBOOK_CLIENT_ID}&redirect_uri=#{callback_url}&scope=offline_access,publish_stream" self.save! end self.oauth_authorize_url end def validate_oauth_token(oauth_verifier, callback_url = '') response = RestClient.get 'https://graph.facebook.com/oauth/access_token', :params => { :client_id => FACEBOOK_CLIENT_ID, :redirect_uri => callback_url.html_safe, :client_secret => FACEBOOK_CLIENT_SECRET, :code => oauth_verifier.html_safe } pair = response.body.split("&")[0].split("=") if (pair[0] == "access_token") self.access_token = pair[1] response = RestClient.get 'https://graph.facebook.com/me', :params => { :access_token => self.access_token } self.stream_url = JSON.parse(response.body)["link"] self.active = true else self.errors.add(:oauth_verifier, "Invalid token, unable to connect to facebook: #{pair[1]}") self.active = false end self.save! end def post(message) RestClient.post 'https://graph.facebook.com/me/feed', { :access_token => self.access_token, :message => message } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ class FacebookAccountsController < ApplicationController # Stubbed out! Does no (good) error checking! def new facebook_account = FacebookAccount.create() redirect_to(facebook_account.authorize_url(facebook_callback_url(:id => facebook_account.id))) end def callback if params[:error_reason] && !params[:error_reason].empty? # We have a problem! redirect_to(:root, :notice => "Unable to activate facebook: #{params[:error_reason]}") elsif params[:code] && !params[:code].empty? # This is the callback, we have an id and an access code facebook_account = FacebookAccount.find(params[:id]) facebook_account.validate_oauth_token(params[:code], facebook_callback_url(:id => facebook_account.id)) redirect_to(:root, :notice => 'Facebook account activated!') 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ resource :facebook_account match '/callback/facebook/:id' => "facebook_accounts#callback", :as => :facebook_callback