Skip to content

Instantly share code, notes, and snippets.

@ckdake
Created November 2, 2010 18:22
Show Gist options
  • Save ckdake/660059 to your computer and use it in GitHub Desktop.
Save ckdake/660059 to your computer and use it in GitHub Desktop.
stub for linking with and posting to facebook from rails apps
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
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
gem 'rest-client'
gem 'json'
resource :facebook_account
match '/callback/facebook/:id' => "facebook_accounts#callback", :as => :facebook_callback
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
@SergXIIIth
Copy link

I have a problem with FacebookAccount.validate_oauth_token method. So I change it

  def validate_oauth_token(oauth_verifier, callback_url = '')
    response = RestClient.post "https://graph.facebook.com/oauth/access_token", client_id: FACEBOOK_CLIENT_ID, redirect_uri: callback_url, client_secret: FACEBOOK_CLIENT_SECRET, code: oauth_verifier

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment