koala 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ v0.7.2
2
+ -- Added support for exchanging session keys for OAuth access tokens (get_token_from_session_key for single keys, get_tokens_from_session_keys for multiple)
3
+ -- Moved Koala files into a koala/ subdirectory to minimize risk of name collisions
4
+ -- Added OAuth Playground git submodule as an example
5
+ -- Updated tests, readme, and changelog
6
+
1
7
  v0.7.1
2
8
  -- Updated RealtimeUpdates#list_subscriptions and GraphAPI#get_connections to now return an
3
9
  array of results directly (rather than a hash with one key)
data/Manifest CHANGED
@@ -1,12 +1,26 @@
1
1
  CHANGELOG
2
2
  Manifest
3
3
  Rakefile
4
+ examples/oauth_playground/Capfile
5
+ examples/oauth_playground/LICENSE
6
+ examples/oauth_playground/Rakefile
7
+ examples/oauth_playground/config.ru
8
+ examples/oauth_playground/config/deploy.rb
9
+ examples/oauth_playground/config/facebook.yml
10
+ examples/oauth_playground/lib/load_facebook.rb
11
+ examples/oauth_playground/lib/oauth_playground.rb
12
+ examples/oauth_playground/readme.md
13
+ examples/oauth_playground/spec/oauth_playground_spec.rb
14
+ examples/oauth_playground/spec/spec_helper.rb
15
+ examples/oauth_playground/tmp/restart.txt
16
+ examples/oauth_playground/views/index.erb
17
+ examples/oauth_playground/views/layout.erb
4
18
  init.rb
5
- lib/graph_api.rb
6
- lib/http_services.rb
7
19
  lib/koala.rb
8
- lib/realtime_updates.rb
9
- lib/rest_api.rb
20
+ lib/koala/graph_api.rb
21
+ lib/koala/http_services.rb
22
+ lib/koala/realtime_updates.rb
23
+ lib/koala/rest_api.rb
10
24
  readme.md
11
25
  spec/facebook_data.yml
12
26
  spec/koala/api_base_tests.rb
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake'
4
4
  require 'echoe'
5
5
 
6
6
  # gem management
7
- Echoe.new('koala', '0.7.1') do |p|
7
+ Echoe.new('koala', '0.7.2') do |p|
8
8
  p.summary = "A lightweight, flexible library for Facebook with support for the Graph API, the old REST API, realtime updates, and OAuth validation."
9
9
  p.description = "Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services."
10
10
  p.url = "http://github.com/arsduo/koala"
@@ -0,0 +1,2 @@
1
+ load 'deploy' if respond_to?(:namespace) # cap2 differentiator
2
+ load 'config/deploy' # remove this line to skip loading any of the default tasks
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010 Alex Koppel
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ namespace :oauth_playground do
2
+
3
+
4
+ end
@@ -0,0 +1,39 @@
1
+ set :application, "oauth_playground"
2
+ set :repository, "git://github.com/arsduo/oauth_playground.git"
3
+ set :domain, "oauth.twoalex.com"
4
+ set :deploy_to, "$HOME/rails_apps/#{application}/"
5
+
6
+ # authentication
7
+ set :scm, "git"
8
+ set :user, "alexkm"
9
+ set :use_sudo, false
10
+ ssh_options[:forward_agent] = true
11
+
12
+ # web server
13
+ role :web, "oauth.twoalex.com" # Your HTTP server, Apache/etc
14
+ role :app, "oauth.twoalex.com" # This may be the same as your `Web` server
15
+ role :db, "oauth.twoalex.com", :primary => true # This is where Rails migrations will run
16
+
17
+
18
+ # other git-related commands
19
+ set :branch, "master"
20
+ default_run_options[:pty] = true
21
+ # cache the repository locally to speed updates
22
+ set :repository_cache, "git_cache"
23
+ set :deploy_via, :remote_cache
24
+
25
+
26
+ # passenger-specific deploy tasks
27
+ namespace :deploy do
28
+ task :start do
29
+ run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
30
+ end
31
+
32
+ task :stop do
33
+ # nothing
34
+ end
35
+
36
+ task :restart, :roles => :app, :except => { :no_release => true } do
37
+ run "touch #{File.join(current_path,'tmp','restart.txt')}"
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ development:
2
+ api_key:
3
+ secret_key:
4
+ app_id:
5
+
6
+ test:
7
+ api_key:
8
+ secret_key:
9
+
10
+ production:
11
+ api_key:
12
+ secret_key:
13
+ app_id:
@@ -0,0 +1,27 @@
1
+ # gems
2
+ require 'sinatra'
3
+ require 'logger'
4
+ require 'yaml'
5
+
6
+ # app files
7
+ require 'koala'
8
+ require File.join(File.dirname(__FILE__), 'lib', 'load_facebook.rb')
9
+ require File.join(File.dirname(__FILE__), 'lib', 'oauth_playground.rb')
10
+
11
+ # LOGGING
12
+ # set up the logfile
13
+ Dir.mkdir('log') unless File.exists?('log')
14
+ log_filename = File.join(File.dirname(__FILE__), "log", "sinatra.log")
15
+ log = File.new(log_filename, "a+")
16
+
17
+ # log requests
18
+ use Rack::CommonLogger, log
19
+ # log application-generated code
20
+ LOGGER = Logger.new(log_filename)
21
+ # log output to stdout and stderr as well
22
+ $stdout.reopen(log)
23
+ $stderr.reopen(log)
24
+
25
+ # activate the app
26
+ disable :run
27
+ run OAuthPlayground
@@ -0,0 +1,3 @@
1
+ # load Facebook info for this environment
2
+ FACEBOOK_INFO = YAML.load_file(File.join(File.dirname(__FILE__), "..", "config", "facebook.yml"))[ENV["RACK_ENV"]]
3
+
@@ -0,0 +1,187 @@
1
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2
+
3
+ require 'rubygems'
4
+ require 'sinatra'
5
+ require 'erb'
6
+
7
+ class OAuthPlayground < Sinatra::Application
8
+
9
+ set :root, APP_ROOT
10
+
11
+ include Koala
12
+
13
+ layout :layout
14
+
15
+ get "/" do
16
+ @app_data = FACEBOOK_INFO.merge("callback_url" => "#{request.scheme}://#{request.host}/")
17
+ @oauth = Facebook::OAuth.new(@app_data["app_id"], @app_data["secret_key"], @app_data["callback_url"])
18
+
19
+ # get authentication info
20
+ set_facebook_cookies
21
+ set_oauth_data
22
+ set_access_token
23
+
24
+ unless (@permissions = params[:permissions]) && @permissions.length > 0
25
+ @active_permissions = (get_active_permissions || {}).inject([]) do |active, perm|
26
+ # collect our active permissions
27
+ active << perm[0].to_sym if perm[1] == 1
28
+ active
29
+ end
30
+ @fetched_permissions = true
31
+ else
32
+ @active_permissions = @permissions.collect {|p| p.to_sym}
33
+ end
34
+
35
+ @available_permissions = [
36
+ {:name => "User Activity", :perms => ACTIVITY_PERMISSIONS},
37
+ {:name => "User Info", :perms => USER_PERMISSIONS},
38
+ {:name => "Friend Info", :perms => FRIEND_PERMISSIONS}
39
+ ]
40
+
41
+ erb :index
42
+ end
43
+
44
+ get "/subscriptions" do
45
+ # validate that this is a valid response
46
+ # it will automatically render the result of the verification
47
+ # e.g. either the challenge phrase or false
48
+ subscription = Facebook::RealtimeUpdates.meet_challenge(params) do |verification_token|
49
+ token_parts = verification_token.split("|")
50
+ expected = Digest::MD5.hexdigest("#{token_parts.first}~koala")
51
+ logger.info "expected: #{expected}"
52
+ logger.info "got: #{token_parts.last}"
53
+ # determine if this is a valid token -- that is, if the send part is a properly encoding of the first
54
+ expected == token_parts.last
55
+ end
56
+ end
57
+
58
+ helpers do
59
+ def logger
60
+ LOGGER
61
+ end
62
+ end
63
+
64
+ # helpers
65
+
66
+ # set up our understanding of the user's session
67
+
68
+ def set_access_token
69
+ # get the access token from wherever we can
70
+ @access_token ||= (set_oauth_data && @oauth_access_token) || (set_facebook_cookies && @cookie_access_token)
71
+ end
72
+
73
+ def set_oauth_data
74
+ unless @oauth_access_token
75
+ if (@code = params[:code]) && @raw_access_response = @oauth.send(:fetch_token_string, {:code => @code, :redirect_uri => @app_data["callback_url"]})
76
+ parsed = @oauth.send(:parse_access_token, @raw_access_response)
77
+ @oauth_access_token = parsed["access_token"]
78
+ @expiration = parsed["expires"] || "Does not expire (offline)"
79
+ end
80
+ end
81
+
82
+ @oauth_access_token
83
+ end
84
+
85
+ def set_facebook_cookies
86
+ unless @facebook_cookies
87
+ if @facebook_cookies = @oauth.get_user_from_cookie(request.cookies)
88
+ @cookie_access_token = @facebook_cookies["access_token"]
89
+ end
90
+ end
91
+
92
+ @facebook_cookies
93
+ end
94
+
95
+ def set_uid
96
+ # get the OAuth data, including fetching the access token, if available and necessary
97
+ # e.g. if we have an OAuth token and no cookie data
98
+ unless @uid
99
+ if @facebook_cookies
100
+ @uid = @facebook_cookies["uid"]
101
+ elsif token = set_access_token
102
+ # we have to fetch the info
103
+ @graph = Facebook::GraphAPI.new(token)
104
+ result = @graph.get_object("me")
105
+ @uid = result["id"]
106
+ end
107
+ end
108
+ @uid
109
+ end
110
+
111
+ # fetch the active permissions about the user
112
+ def get_active_permissions
113
+ set_access_token
114
+ if @access_token && !@permissions && set_uid
115
+ # if we don't have permissions set but have an access token
116
+ # grab the user's info
117
+ @rest = Facebook::RestAPI.new(@access_token)
118
+ result = @rest.fql_query("select #{all_permissions.join(",")} from permissions where uid = #{@uid.to_s}")
119
+ result.first
120
+ end
121
+ end
122
+
123
+ # list of permissions
124
+
125
+ def all_permissions
126
+ ACTIVITY_PERMISSIONS + USER_PERMISSIONS + FRIEND_PERMISSIONS
127
+ end
128
+
129
+ ACTIVITY_PERMISSIONS = [
130
+ :publish_stream, # Enables your application to post content, comments, and likes to a user's stream and to the streams of the user's friends, without prompting the user each time.
131
+ :create_event, # Enables your application to create and modify events on the user's behalf
132
+ :rsvp_event, # Enables your application to RSVP to events on the user's behalf
133
+ :sms, # Enables your application to send messages to the user and respond to messages from the user via text message
134
+ :offline_access # Enables your application to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.
135
+ ]
136
+
137
+ USER_PERMISSIONS = [
138
+ :email, # Provides access to the user's primary email address in the email property. Do not spam users. Your use of email must comply both with Facebook policies and with the CAN-SPAM Act.
139
+ :read_insights, # Provides read access to the Insights data for pages, applications, and domains the user owns.
140
+ :read_stream, # Provides access to all the posts in the user's News Feed and enables your application to perform searches against the user's News Feed
141
+ :user_about_me, # Provides access to the "About Me" section of the profile in the about property
142
+ :user_activities, # Provides access to the user's list of activities as the activities connection
143
+ :user_birthday, # Provides access to the full birthday with year as the birthday_date property
144
+ :user_education_history, # Provides access to education history as the education property
145
+ :user_events, # Provides access to the list of events the user is attending as the events connection
146
+ :user_groups, # Provides access to the list of groups the user is a member of as the groups connection
147
+ :user_hometown, # Provides access to the user's hometown in the hometown property
148
+ :user_interests, # Provides access to the user's list of interests as the interests connection
149
+ :user_likes, # Provides access to the list of all of the pages the user has liked as the likes connection
150
+ :user_location, # Provides access to the user's current location as the current_location property
151
+ :user_notes, # Provides access to the user's notes as the notes connection
152
+ :user_online_presence, # Provides access to the user's online/offline presence
153
+ :user_photo_video_tags, # Provides access to the photos the user has been tagged in as the photos connection
154
+ :user_photos, # Provides access to the photos the user has uploaded
155
+ :user_relationships, # Provides access to the user's family and personal relationships and relationship status
156
+ :user_religion_politics, # Provides access to the user's religious and political affiliations
157
+ :user_status, # Provides access to the user's most recent status message
158
+ :user_videos, # Provides access to the videos the user has uploaded
159
+ :user_website, # Provides access to the user's web site URL
160
+ :user_work_history # Provides access to work history as the work property
161
+ ]
162
+
163
+ FRIEND_PERMISSIONS = [
164
+ :read_friendlists, # Provides read access to the user's friend lists
165
+ :read_requests, # Provides read access to the user's friend requests
166
+ :friends_about_me, # Provides access to the "About Me" section of the profile in the about property
167
+ :friends_activities, # Provides access to the user's list of activities as the activities connection
168
+ :friends_birthday, # Provides access to the full birthday with year as the birthday_date property
169
+ :friends_education_history, # Provides access to education history as the education property
170
+ :friends_events, # Provides access to the list of events the user is attending as the events connection
171
+ :friends_groups, # Provides access to the list of groups the user is a member of as the groups connection
172
+ :friends_hometown, # Provides access to the user's hometown in the hometown property
173
+ :friends_interests, # Provides access to the user's list of interests as the interests connection
174
+ :friends_likes, # Provides access to the list of all of the pages the user has liked as the likes connection
175
+ :friends_location, # Provides access to the user's current location as the current_location property
176
+ :friends_notes, # Provides access to the user's notes as the notes connection
177
+ :friends_online_presence, # Provides access to the user's online/offline presence
178
+ :friends_photo_video_tags, # Provides access to the photos the user has been tagged in as the photos connection
179
+ :friends_photos, # Provides access to the photos the user has uploaded
180
+ :friends_relationships, # Provides access to the user's family and personal relationships and relationship status
181
+ :friends_religion_politics, # Provides access to the user's religious and political affiliations
182
+ :friends_status, # Provides access to the user's most recent status message
183
+ :friends_videos, # Provides access to the videos the user has uploaded
184
+ :friends_website, # Provides access to the user's web site URL
185
+ :friends_work_history # Provides access to work history as the work property
186
+ ]
187
+ end
@@ -0,0 +1,8 @@
1
+ A simple OAuth Playground chock full of the info you need to test your OAuth-based Facebook application.
2
+
3
+ To Do's
4
+ =======
5
+
6
+ * Extend the permissions controls to cover all available permissions
7
+ * Make expiration dates human-readable
8
+ * Let people plug in their own app (updating the app's connect properties through setAppProperties)
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'ruby-debug'
3
+
4
+ describe 'OAuthPlayground' do
5
+ before :each do
6
+ @hydra = Typhoeus::Hydra.hydra
7
+ end
8
+
9
+ after :each do
10
+ @hydra.clear_stubs
11
+ end
12
+
13
+ it 'should load the index' do
14
+ get '/'
15
+ last_response.should be_ok
16
+ end
17
+
18
+ =begin
19
+ # unfortunately, this fails when you pass the get method a param named code!
20
+ # fixing this will require some mucking around in Rack::Test
21
+
22
+ it "should make a request to Facebook's OAuth server when passed a code" do
23
+ test_string = Regexp.new("The time is #{Time.now.to_i}")
24
+
25
+ # stub out the request and make sure it's returned
26
+ @hydra.stub("https://#{Koala::Facebook::GRAPH_SERVER}/oauth/access_token", "get").and_return(test_string)
27
+
28
+ get "/", {"code" => "foo_bar"}
29
+
30
+ # make sure the body includes the request string
31
+ last_response.body.should =~ test_string
32
+ end
33
+ =end
34
+ end
35
+
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'rack/test'
4
+ require 'typhoeus'
5
+ require 'koala'
6
+
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'oauth_playground.rb')
8
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'load_facebook.rb')
9
+
10
+ OAuthPlayground.set(
11
+ :environment => :test,
12
+ :run => false,
13
+ :raise_errors => true,
14
+ :logging => false
15
+ )
16
+
17
+ module TestHelper
18
+
19
+ def app
20
+ # change to your app class if using the 'classy' style
21
+ OAuthPlayground
22
+ end
23
+
24
+ def body
25
+ last_response.body
26
+ end
27
+
28
+ def status
29
+ last_response.status
30
+ end
31
+
32
+ include Rack::Test::Methods
33
+
34
+ end
35
+
36
+ include TestHelper
File without changes
@@ -0,0 +1,206 @@
1
+ <div id="header">
2
+ <h1>Facebook OAuth Playground</h1>
3
+ <h4>Powered by <a href="http://github.com/arsduo/koala" target="_blank">Koala</a></h2>
4
+ <h4>For novelty use only. Remember, the application's secret key is public.</h4>
5
+ </div>
6
+
7
+ <style>
8
+ #header {
9
+ text-align: center;
10
+ }
11
+
12
+ h1, h2, h3, h4 { margin: 0; }
13
+
14
+ .section {
15
+ border: 1px solid black;
16
+ padding: 10px;
17
+ -moz-border-radius: 9px;
18
+ -webkit-border-radius: 9px;
19
+ margin-bottom: 10px;
20
+ width: 100%;
21
+ }
22
+
23
+ #contents {
24
+ border: 2px solid #CCC;
25
+ border-width: 2px 0;
26
+ background-color: #EEE;
27
+ padding: 14px 3px 8px 8px;
28
+ }
29
+
30
+ #configurationInfo {
31
+ float:left;
32
+ margin-right: 2.5%;
33
+ width: 25%;
34
+ }
35
+
36
+ #configurationInfo .section {
37
+ background-color: #CCC;
38
+ }
39
+
40
+ #permissions .header {
41
+ margin-bottom: 5px;
42
+ }
43
+
44
+ #permissions .list {
45
+ height: 380px;
46
+ overflow: auto;
47
+ }
48
+
49
+ #generatedInfo {
50
+ float: left;
51
+ width: 70%;
52
+ }
53
+
54
+ #generatedInfo .section {
55
+ border-color: gray;
56
+ background: #DDD;
57
+ overflow: auto;
58
+ }
59
+
60
+ .clearFloat {
61
+ height: 1px;
62
+ height: 0px;
63
+ clear: both;
64
+ overflow: hidden;
65
+ }
66
+
67
+ ul, li { margin: 0; padding: 0; list-style: none; }
68
+ ul { margin: 12px 0; }
69
+
70
+ .code {
71
+ font-family: Courier, fixed;
72
+ font-size: 1.15em;
73
+ width: 100%;
74
+ }
75
+
76
+ .explanation {
77
+ font-size: 0.9em;
78
+ }
79
+
80
+ .datumName {
81
+ font-weight: bold;
82
+ }
83
+
84
+ .permissionType {
85
+ font-style: italic;
86
+ }
87
+ </style>
88
+
89
+ </style>
90
+
91
+ <div id="contents">
92
+
93
+ <div id="configurationInfo">
94
+ <div id="fbApp" class="section">
95
+ <h3>Facebook App Info</h3>
96
+ <ul>
97
+ <% @app_data.each_pair do |key, value| %>
98
+ <li><span class="datumName"><%= key %>:</span> <%= value %></li>
99
+ <% end %>
100
+ </ul>
101
+ </div>
102
+
103
+ <div id="permissions" class="section">
104
+ <form method="get">
105
+ <div class="header">
106
+ <h3>Permissions</h3>
107
+ <input type="submit" value="Update permissions" />
108
+ <a href="/">Reset Selection</a>
109
+ <div class="explanation">Choose permissions for the OAuth URL and the fb:login button.</div>
110
+ <% if @fetched_permissions %>
111
+ <div class="permissionType">Showing currently-active permissions</div>
112
+ <div class="explanation">(These were fetched by Koala as the page loaded!)</div>
113
+ <% else %>
114
+ <div class="permissionType">Showing selected permissions</div>
115
+ <% end %>
116
+ </div>
117
+
118
+ <div class="list">
119
+ <% @available_permissions.each do |permissions| %>
120
+ <h4><%= permissions[:name] %> Permissions</h4>
121
+ <ul>
122
+ <% permissions[:perms].each do |p| %>
123
+ <li>
124
+ <input type="checkbox" id="permission<%= p %>" name="permissions[]" value="<%= p %>" <%= @active_permissions.include?(p) ? "checked='checked'" : "" %> />
125
+ <label for="permission<%= p %>"><%= p %></a> (<a href="#" onclick="FB.api({method: 'auth.revokeExtendedPermission', perm: '<%= p %>'}, function(response) { alert(response) }); return false;">revoke</a>)
126
+ </li>
127
+ <% end %>
128
+ </ul>
129
+ <% end %>
130
+ </div>
131
+ </form>
132
+ </div>
133
+ </div>
134
+
135
+ <div id="generatedInfo">
136
+ <div id="oauthURLs" class="section">
137
+ <h3>OAuth URLs</h3>
138
+ <ul>
139
+ <li><span class="datumName">Generate a code:</span> <a href="<%= @oauth.url_for_oauth_code(:permissions => @permissions) %>"><%= @oauth.url_for_oauth_code(:permissions => @permissions) %></a></li>
140
+ <li><span class="datumName">OAuth code:</span> <%= @code || "click on the link above" %></li>
141
+ <li>
142
+ <span class="datumName">Access token:</span> <%= @oauth_access_token || "click on the link above" %>
143
+ <% if @oauth_access_token %><div class="explanation">This was fetched by Koala as the page loaded!</div><% end %>
144
+ </li>
145
+ <li><span class="datumName">Expiration:</span> <%= @expiration || "click on the link above" %></li>
146
+ <li><span class="datumName">Raw access response:</span> <%= @raw_access_response || "click on the link above" %></li>
147
+ <li><span class="datumName">URL for access code:</span>
148
+ <% if @code %>
149
+ <a href="<%= @oauth.url_for_access_token(@code) %>"><%= @oauth.url_for_access_token(@code) %></a>
150
+ <% else %>
151
+ click on the link above
152
+ <% end %>
153
+ </li>
154
+ </ul>
155
+ </div>
156
+
157
+ <div id="jsLogin" class="section">
158
+ <h3>Javascript Login (e.g. Facebook Connect)</h3>
159
+ <p>
160
+ <fb:login-button onlogin="location.reload()" perms="<%= (@permissions || []).join(",") %>"></fb:login-button>
161
+ <% if @permissions %>
162
+ and prompt for <%= @permissions.join(", ") %>
163
+ <% end %>
164
+ <% if @facebook_cookies %>
165
+ <div class="logout">
166
+ <a href="#" onclick="FB.logout(function() { location.reload() }); return false;">Logout</a>
167
+ </div>
168
+ <% end %>
169
+ </p>
170
+ </div>
171
+
172
+ <div id="cookieInfo" class="section">
173
+ <h3>Cookie info</h3>
174
+ <ul>
175
+ <% if @facebook_cookies %>
176
+ <% @facebook_cookies.each_pair do |key, value| %>
177
+ <li><span class="datumName"><%= key %>:</span> <%= value %></li>
178
+ <% end %>
179
+ <% else %>
180
+ <li>You're not signed in via Javascript. Login below.</li>
181
+ <% end %>
182
+ <li><span class="datumName">Raw hash</span>:
183
+ <div class="code"><%= request.cookies.inspect %></div>
184
+ </li>
185
+ </ul>
186
+ </div>
187
+
188
+ <div id="koala" class="section">
189
+ <h3>Koala</h3>
190
+ <ul>
191
+ <li><span class="datumName">GraphAPI:</span>
192
+ <div class="code"><%= @access_token ? "@graph = Koala::Facebook::GraphAPI.new(\"#{@access_token}\")" : "sign in above" %></div>
193
+ </li>
194
+ <li><span class="datumName">OAuth:</span>
195
+ <div class="code">@oauth = Koala::Facebook::OAuth.new(<%= @app_data["app_id"] %>, "<%= @app_data["secret_key"] %>", "<%= @app_data["callback_url"] %>")</div>
196
+ </li>
197
+ </ul>
198
+ </div>
199
+ </div>
200
+
201
+ <div class="clearFloat">&nbsp;</div>
202
+ </div>
203
+
204
+ <center>
205
+ <h5>Check out the playground's code at <a href="http://github.com/arsduo/oauth_playground">http://github.com/arsduo/oauth_playground</a>!</h5>
206
+ </center>
@@ -0,0 +1,39 @@
1
+ <html>
2
+ <head>
3
+ <title>Facebook OAuth Playground</title>
4
+ <meta name="description" content="Making it easier to play with Facebook's new Graph API and OAuth authentication."></meta>
5
+ <style>
6
+ body {
7
+ font-family: verdana, arial, sans-serif;
8
+ font-size: 12px;
9
+ margin: 0px;
10
+ }
11
+ </style>
12
+ </head>
13
+ <body>
14
+
15
+ <script type="text/javascript">
16
+
17
+ var _gaq = _gaq || [];
18
+ _gaq.push(['_setAccount', 'UA-16395421-1']);
19
+ _gaq.push(['_trackPageview']);
20
+
21
+ (function() {
22
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
23
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
24
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
25
+ })();
26
+
27
+ </script>
28
+
29
+ <%= yield %>
30
+
31
+ <div id="fb-root"></div>
32
+ <script src="http://connect.facebook.net/en_US/all.js"></script>
33
+ <script>
34
+ FB.init({ appId: <%= @app_data["app_id"] %>, cookie: true, status: true, xfbml: true });
35
+ </script>
36
+
37
+ </body>
38
+
39
+ </html>
data/koala.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{koala}
5
- s.version = "0.7.1"
5
+ s.version = "0.7.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional"]
9
- s.date = %q{2010-05-27}
9
+ s.date = %q{2010-06-02}
10
10
  s.description = %q{Koala is a lightweight, flexible Ruby SDK for Facebook. It allows read/write access to the social graph via the Graph API and the older REST API, as well as support for realtime updates and OAuth and Facebook Connect authentication. Koala is fully tested and supports Net::HTTP and Typhoeus connections out of the box and can accept custom modules for other services.}
11
11
  s.email = %q{alex@alexkoppel.com}
12
- s.extra_rdoc_files = ["CHANGELOG", "lib/graph_api.rb", "lib/http_services.rb", "lib/koala.rb", "lib/realtime_updates.rb", "lib/rest_api.rb"]
13
- s.files = ["CHANGELOG", "Manifest", "Rakefile", "init.rb", "lib/graph_api.rb", "lib/http_services.rb", "lib/koala.rb", "lib/realtime_updates.rb", "lib/rest_api.rb", "readme.md", "spec/facebook_data.yml", "spec/koala/api_base_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb", "spec/koala/graph_api/graph_api_no_access_token_tests.rb", "spec/koala/graph_api/graph_api_with_access_token_tests.rb", "spec/koala/live_testing_data_helper.rb", "spec/koala/net_http_service_tests.rb", "spec/koala/oauth/oauth_tests.rb", "spec/koala/realtime_updates/realtime_updates_tests.rb", "spec/koala/rest_api/rest_api_no_access_token_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala_spec.rb", "spec/koala_spec_helper.rb", "spec/koala_spec_without_mocks.rb", "spec/mock_facebook_responses.yml", "spec/mock_http_service.rb", "koala.gemspec"]
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb"]
13
+ s.files = ["CHANGELOG", "Manifest", "Rakefile", "examples/oauth_playground/Capfile", "examples/oauth_playground/LICENSE", "examples/oauth_playground/Rakefile", "examples/oauth_playground/config.ru", "examples/oauth_playground/config/deploy.rb", "examples/oauth_playground/config/facebook.yml", "examples/oauth_playground/lib/load_facebook.rb", "examples/oauth_playground/lib/oauth_playground.rb", "examples/oauth_playground/readme.md", "examples/oauth_playground/spec/oauth_playground_spec.rb", "examples/oauth_playground/spec/spec_helper.rb", "examples/oauth_playground/tmp/restart.txt", "examples/oauth_playground/views/index.erb", "examples/oauth_playground/views/layout.erb", "init.rb", "lib/koala.rb", "lib/koala/graph_api.rb", "lib/koala/http_services.rb", "lib/koala/realtime_updates.rb", "lib/koala/rest_api.rb", "readme.md", "spec/facebook_data.yml", "spec/koala/api_base_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb", "spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb", "spec/koala/graph_api/graph_api_no_access_token_tests.rb", "spec/koala/graph_api/graph_api_with_access_token_tests.rb", "spec/koala/live_testing_data_helper.rb", "spec/koala/net_http_service_tests.rb", "spec/koala/oauth/oauth_tests.rb", "spec/koala/realtime_updates/realtime_updates_tests.rb", "spec/koala/rest_api/rest_api_no_access_token_tests.rb", "spec/koala/rest_api/rest_api_with_access_token_tests.rb", "spec/koala_spec.rb", "spec/koala_spec_helper.rb", "spec/koala_spec_without_mocks.rb", "spec/mock_facebook_responses.yml", "spec/mock_http_service.rb", "koala.gemspec"]
14
14
  s.homepage = %q{http://github.com/arsduo/koala}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Koala", "--main", "readme.md"]
16
16
  s.require_paths = ["lib"]
File without changes
File without changes
File without changes
data/lib/koala.rb CHANGED
@@ -6,15 +6,15 @@ require 'rubygems'
6
6
  require 'json'
7
7
 
8
8
  # include default http services
9
- require 'http_services'
9
+ require 'koala/http_services'
10
10
 
11
11
  # add Graph API methods
12
- require 'graph_api'
12
+ require 'koala/graph_api'
13
13
 
14
14
  # add REST API methods
15
- require 'rest_api'
15
+ require 'koala/rest_api'
16
16
 
17
- require 'realtime_updates'
17
+ require 'koala/realtime_updates'
18
18
 
19
19
  module Koala
20
20
 
@@ -156,7 +156,7 @@ module Koala
156
156
  raise ArgumentError, "url_for_access_token must get a callback either from the OAuth object or in the parameters!" unless callback
157
157
  "https://#{GRAPH_SERVER}/oauth/access_token?client_id=#{@app_id}&redirect_uri=#{callback}&client_secret=#{@app_secret}&code=#{code}"
158
158
  end
159
-
159
+
160
160
  def get_access_token(code)
161
161
  # convenience method to get a parsed token from Facebook for a given code
162
162
  # should this require an OAuth callback URL?
@@ -168,6 +168,27 @@ module Koala
168
168
  get_token_from_server({:type => 'client_cred'}, true)
169
169
  end
170
170
 
171
+ def get_tokens_from_session_keys(sessions)
172
+ # fetch the OAuth tokens from Facebook
173
+ response = fetch_token_string({
174
+ :type => 'client_cred',
175
+ :sessions => sessions.join(",")
176
+ }, true, "exchange_sessions")
177
+
178
+ # get_token_from_session_key should return an empty body if an empty string or nil is provided
179
+ # if invalid tokens are provided, it returns an array of nulls, which is a valid result
180
+ if response == ""
181
+ raise APIError.new("ArgumentError", "get_token_from_session_key received an error (empty response body) for sessions #{sessions.inspect}!")
182
+ end
183
+
184
+ JSON.parse(response)
185
+ end
186
+
187
+ def get_token_from_session_key(session)
188
+ # convenience method for a single key
189
+ get_tokens_from_session_keys([session])[0]
190
+ end
191
+
171
192
  protected
172
193
 
173
194
  def get_token_from_server(args, post = false)
@@ -189,8 +210,8 @@ module Koala
189
210
  components
190
211
  end
191
212
 
192
- def fetch_token_string(args, post = false)
193
- Koala.make_request("oauth/access_token", {
213
+ def fetch_token_string(args, post = false, endpoint = "access_token")
214
+ Koala.make_request("oauth/#{endpoint}", {
194
215
  :client_id => @app_id,
195
216
  :client_secret => @app_secret
196
217
  }.merge!(args), post ? "post" : "get").body
data/readme.md CHANGED
@@ -34,7 +34,7 @@ We reserve the right to expand the built-in REST API coverage to additional conv
34
34
  OAuth
35
35
  -----
36
36
  You can use the Graph and REST APIs without an OAuth access token, but the real magic happens when you provide Facebook an OAuth token to prove you're authenticated. Koala provides an OAuth class to make that process easy:
37
- @oauth = Koala::Facebook::OAuth.new(app_id, code, callback_url)
37
+ @oauth = Koala::Facebook::OAuth.new(app_id, code, callback_url)
38
38
 
39
39
  If your application uses Koala and the Facebook [JavaScript SDK](http://github.com/facebook/connect-js) (formerly Facebook Connect), you can use the OAuth class to parse the cookies:
40
40
  @oauth.get_user_from_cookie(cookies)
@@ -50,6 +50,9 @@ You can also get your application's own access token, which can be used without
50
50
 
51
51
  That's it! It's pretty simple once you get the hang of it. If you're new to OAuth, though, check out the wiki and the OAuth Playground example site (see below).
52
52
 
53
+ *Exchanging session keys:* Stuck building tab applications on Facebook? Wishing you had an OAuth token so you could use the Graph API? You're in luck! Koala now allows you to exchange session keys for OAuth access tokens:
54
+ @oauth.get_token_from_session_key(session_key)
55
+ @oauth.get_tokens_from_session_keys(array_of_session_keys)
53
56
 
54
57
  Real-time Updates
55
58
  -----
@@ -5,14 +5,19 @@
5
5
 
6
6
  # You must supply this value yourself to test the GraphAPI class.
7
7
  # Your OAuth token should have publish_stream and read_stream permissions.
8
- oauth_token: 119908831367602|2.6WkxQTbQPGFCyLblPYdsMg__.3600.1274979600-2905623|8YPVs-jBmpWC6y10pMCWzMRFrdk.
8
+ oauth_token: 119908831367602|2.r_nk_Ghvmy2OVfCqc2xD5w__.3600.1275534000-2905623|uRgZZ9WrZL7sBmS2f02G7UpMFZg.
9
9
 
10
10
  # for testing the OAuth class
11
11
  # baseline app
12
12
  oauth_test_data:
13
13
  # You must supply this value yourself, since they will expire.
14
- code: 2.6WkxQTbQPGFCyLblPYdsMg__.3600.1274979600-2905623|zHzbqwoONeqNxOXX7z9LZlpEGdc.
15
-
14
+ code: 2.r_nk_Ghvmy2OVfCqc2xD5w__.3600.1275534000-2905623|4nUNPLdAgG0Q7PauPJ00TffIB88.
15
+ # easiest way to get session keys: use multiple test accounts with the Javascript login at http://oauth.twoalex.com
16
+ session_key: 2.r_nk_Ghvmy2OVfCqc2xD5w__.3600.1275534000-2905623
17
+ multiple_session_keys:
18
+ - 2.r_nk_Ghvmy2OVfCqc2xD5w__.3600.1275534000-2905623
19
+ - 2.r_nk2_Ghvmy2OVfCqc2xD5w__.3600.1275534000-2905623
20
+
16
21
  # These values will work out of the box
17
22
  app_id: 119908831367602
18
23
  secret: e45e55a333eec232d4206d2703de1307
@@ -1,4 +1,4 @@
1
- require 'http_services'
1
+ require 'koala/http_services'
2
2
 
3
3
  class NetHTTPServiceTests < Test::Unit::TestCase
4
4
  module Bear
@@ -157,8 +157,6 @@ class FacebookOAuthTests < Test::Unit::TestCase
157
157
  out.should_not be_nil
158
158
  end
159
159
 
160
- # START CODE THAT NEEDS MOCKING
161
-
162
160
  # get_access_token
163
161
  it "should properly get and parse an access token token results" do
164
162
  result = @oauth.get_access_token(@code)
@@ -169,10 +167,37 @@ class FacebookOAuthTests < Test::Unit::TestCase
169
167
  lambda { @oauth.get_access_token("foo") }.should raise_error(Koala::Facebook::APIError)
170
168
  end
171
169
 
170
+ # get_app_access_token
171
+
172
172
  it "should properly get and parse an app's access token token results" do
173
173
  result = @oauth.get_app_access_token
174
174
  result["access_token"].should
175
175
  end
176
+
177
+ # get_tokens_from_session_keys
178
+ it "should get an array of session keys from Facebook when passed a single key" do
179
+ result = @oauth.get_tokens_from_session_keys([@oauth_data["session_key"]])
180
+ result.should be_an(Array)
181
+ result.length.should == 1
182
+ end
183
+
184
+ it "should get an array of session keys from Facebook when passed multiple keys" do
185
+ result = @oauth.get_tokens_from_session_keys(@oauth_data["multiple_session_keys"])
186
+ result.should be_an(Array)
187
+ result.length.should == 2
188
+ end
189
+
190
+ # get_token_from_session_key
191
+ it "should call get_tokens_from_session_keys when the get_token_from_session_key is called" do
192
+ key = @oauth_data["session_key"]
193
+ @oauth.should_receive(:get_tokens_from_session_keys).with([key]).and_return([])
194
+ @oauth.get_token_from_session_key(key)
195
+ end
196
+
197
+ it "should get back a hash from get_token_from_session_key" do
198
+ result = @oauth.get_token_from_session_key(@oauth_data["session_key"])
199
+ result["access_token"].should
200
+ end
176
201
 
177
202
  # protected methods
178
203
  # since these are pretty fundamental and pretty testable, we want to test them
@@ -136,7 +136,15 @@ graph_api:
136
136
  client_id=<%= APP_ID %>&client_secret=<%= SECRET %>&type=client_cred:
137
137
  post:
138
138
  no_token: access_token=<%= ACCESS_TOKEN %>
139
-
139
+ oauth/exchange_sessions:
140
+ client_id=<%= APP_ID %>&client_secret=<%= SECRET %>&sessions=<%= OAUTH_DATA["session_key"] %>&type=client_cred:
141
+ post:
142
+ no_token: '[{"access_token":"<%= ACCESS_TOKEN %>","expires":4315}]'
143
+ client_id=<%= APP_ID %>&client_secret=<%= SECRET %>&sessions=<%= OAUTH_DATA["multiple_session_keys"].join(",") %>&type=client_cred:
144
+ post:
145
+ no_token: '[{"access_token":"<%= ACCESS_TOKEN %>","expires":4315}, {"access_token":"<%= ACCESS_TOKEN %>","expires":4315}]'
146
+
147
+
140
148
 
141
149
  # -- Subscription Responses --
142
150
  <%= APP_ID %>/subscriptions:
@@ -15,7 +15,11 @@ module Koala
15
15
 
16
16
  # Useful in mock_facebook_responses.yml
17
17
  OAUTH_DATA = TEST_DATA['oauth_test_data']
18
- OAUTH_DATA.merge!('app_access_token' => Koala::MockHTTPService::ACCESS_TOKEN)
18
+ OAUTH_DATA.merge!({
19
+ 'app_access_token' => Koala::MockHTTPService::ACCESS_TOKEN,
20
+ 'session_key' => "session_key",
21
+ 'multiple_session_keys' => ["session_key", "session_key_2"]
22
+ })
19
23
  APP_ID = OAUTH_DATA['app_id']
20
24
  SECRET = OAUTH_DATA['secret']
21
25
  SUBSCRIPTION_DATA = TEST_DATA["subscription_test_data"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 1
9
- version: 0.7.1
8
+ - 2
9
+ version: 0.7.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alex Koppel, Chris Baclig, Rafi Jacoby, Context Optional
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-27 00:00:00 -07:00
17
+ date: 2010-06-02 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -26,21 +26,35 @@ extensions: []
26
26
 
27
27
  extra_rdoc_files:
28
28
  - CHANGELOG
29
- - lib/graph_api.rb
30
- - lib/http_services.rb
31
29
  - lib/koala.rb
32
- - lib/realtime_updates.rb
33
- - lib/rest_api.rb
30
+ - lib/koala/graph_api.rb
31
+ - lib/koala/http_services.rb
32
+ - lib/koala/realtime_updates.rb
33
+ - lib/koala/rest_api.rb
34
34
  files:
35
35
  - CHANGELOG
36
36
  - Manifest
37
37
  - Rakefile
38
+ - examples/oauth_playground/Capfile
39
+ - examples/oauth_playground/LICENSE
40
+ - examples/oauth_playground/Rakefile
41
+ - examples/oauth_playground/config.ru
42
+ - examples/oauth_playground/config/deploy.rb
43
+ - examples/oauth_playground/config/facebook.yml
44
+ - examples/oauth_playground/lib/load_facebook.rb
45
+ - examples/oauth_playground/lib/oauth_playground.rb
46
+ - examples/oauth_playground/readme.md
47
+ - examples/oauth_playground/spec/oauth_playground_spec.rb
48
+ - examples/oauth_playground/spec/spec_helper.rb
49
+ - examples/oauth_playground/tmp/restart.txt
50
+ - examples/oauth_playground/views/index.erb
51
+ - examples/oauth_playground/views/layout.erb
38
52
  - init.rb
39
- - lib/graph_api.rb
40
- - lib/http_services.rb
41
53
  - lib/koala.rb
42
- - lib/realtime_updates.rb
43
- - lib/rest_api.rb
54
+ - lib/koala/graph_api.rb
55
+ - lib/koala/http_services.rb
56
+ - lib/koala/realtime_updates.rb
57
+ - lib/koala/rest_api.rb
44
58
  - readme.md
45
59
  - spec/facebook_data.yml
46
60
  - spec/koala/api_base_tests.rb