Skip to content

Commit

Permalink
Added support for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed May 3, 2010
1 parent b175e05 commit 10f533c
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 18 deletions.
58 changes: 43 additions & 15 deletions Readme
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,64 @@ For documentation on the Open Graph Library, see: http://developers.facebook.com
Quick Start:
======================================

* create a token by following the instructions at http://developers.facebook.com/docs/authentication/desktop
Add config.gem "mogli" to environment.rb

For Rails: create a controller like the following:

class OauthController < ApplicationController
def new
session[:access_token]=nil
redirect_to authenticator.authorize_url
end

def create
mogli_client = Mogli::Client.create_from_code_and_authenticator(params[:code],authenticator)
session[:at]=mogli_client.access_token
redirect_to "/"
end

def index
redirect_to new_oauth_path and return unless session[:at]
user = Mogli::User.find("me",Mogli::Client.new(session[:at]))
@user = user
@posts = user.posts
end

def authenticator
@authenticator ||= Mogli::Authenticator.new('client_id',
'secret',
oauth_callback_url)
end
end


with routes:

map.resource :oauth, :controller=>"oauth"
map.root :controller=>"oauth"
map.oauth_callback "/oauth/create", :controller=>"oauth", :action=>"create"

Viewing / should redirect you to the login page, and then redirect back to your app to show your recent posts

From the console, you can create a client with the stored access token:

The important part is to make a request to https://graph.facebook.com/oauth/authorize?client_id=...&redirect_uri=http://www.facebook.com/connect/login_success.html&type=user_agent&display=popup

(they have typos in the tutorial)

grab the result and copy the access_token parameter. This will need to be URL decoded.

With the decoded access_token, create a new Mogli client:

in the mogli directory, run
irb -Ilib

require "rubygems"
require "init"
require "mogli"
client = Mogli::Client.new("your_access_token")

You can now fetch users with the client, for example:

myself = client.user("me")
myself = Mogli::User.find("me",client)

or

mikemangino = client.user("12451752")
mikemangino = Mogli::User.find(12451752,client)

When you fetch yourself, you can look at your posts and other information:

myself.posts

Unfortunately, quite a bit of information is unavailable due to a facebook bug. If you want to see this fixed, please vote for: http://bugs.developers.facebook.com/show_bug.cgi?id=9864

You can also fetch other objects by ID, for example:

Expand Down
4 changes: 3 additions & 1 deletion Todo.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

1) allow embedding into other classes, example:
*) add scopes into authentication url stuff
*) Add expiration time handling
*) allow embedding into other classes, example:

class User < ActiveRecord::Base
acts_as_ogli :id=>:facebook_id,:class=>Ogli::User
Expand Down
1 change: 1 addition & 0 deletions lib/mogli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Mogli
require "hashie"


require "mogli/authenticator"
require "mogli/model"
require "mogli/fetching_array"
require "mogli/action"
Expand Down
21 changes: 21 additions & 0 deletions lib/mogli/authenticator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "cgi"
module Mogli
class Authenticator
attr_reader :client_id, :secret, :callback_url

def initialize(client_id,secret,callback_url)
@client_id = client_id
@secret = secret
@callback_url = callback_url
end

def authorize_url
"https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}"
end

def access_token_url(code)
"https://graph.facebook.com/oauth/access_token?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}&client_secret=#{secret}&code=#{CGI.escape(code)}"
end

end
end
15 changes: 14 additions & 1 deletion lib/mogli/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ def initialize(access_token = nil)
@default_params = @access_token ? {:access_token=>access_token} : {}
end

def self.get_access_token_for_code_and_authenticator(code,authenticator)
post_data = get(authenticator.access_token_url(code))
parts = post_data.split("&")
hash = {}
parts.each do |p| (k,v) = p.split("=")
hash[k]=v
end
hash["access_token"]
end

def self.create_from_code_and_authenticator(code,authenticator)
new(get_access_token_for_code_and_authenticator(code,authenticator))
end

def get_and_map(path,klass=nil)
data = self.class.get(api_path(path),:query=>default_params)
map_data(data,klass)
Expand All @@ -36,7 +50,6 @@ def map_data(data,klass=nil)
hash_or_array
end


#protected

def extract_hash_or_array(hash_or_array,klass)
Expand Down
2 changes: 1 addition & 1 deletion mogli.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'mogli'
s.version = '0.0.1'
s.version = '0.0.2'
s.summary = "Open Graph Library for Ruby"
s.description = %{Simple library for accessing the facebook Open Graph API}
s.files = Dir['lib/**/*.rb']
Expand Down
27 changes: 27 additions & 0 deletions spec/authenticator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"
describe Mogli::Authenticator do

let :authenticator do
Mogli::Authenticator.new("123456","secret","http://example.com/url")
end

it "has the client id" do
authenticator.client_id.should == "123456"
end

it "has the secret" do
authenticator.secret.should == "secret"
end

it "has the callback url" do
authenticator.callback_url.should == "http://example.com/url"
end

it "creates the authorize_url" do
authenticator.authorize_url.should == "https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl"
end

it "creates the access_token_url" do
authenticator.access_token_url("mycode").should == "https://graph.facebook.com/oauth/access_token?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&client_secret=secret&code=mycode"
end
end
7 changes: 7 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
client = Mogli::Client.new
client.default_params.should == {}
end

it "create get access token from an authenticator and code" do
Mogli::Client.should_receive(:get).with("url").and_return("access_token=114355055262088|2.6_s8VD_HRneAq3_tUEHJhA__.3600.1272920400-12451752|udZzWly7ptI7IMgX7KTdzaoDrhU.&expires=4168")
Mogli::Client.get_access_token_for_code_and_authenticator("code",mock("auth",:access_token_url=>"url")).should == "114355055262088|2.6_s8VD_HRneAq3_tUEHJhA__.3600.1272920400-12451752|udZzWly7ptI7IMgX7KTdzaoDrhU."

end

end

describe "Making requests" do
Expand Down

0 comments on commit 10f533c

Please sign in to comment.