Skip to content
Browse files

Merge branch 'ads_api' of https://github.com/blinqmedia/fb_graph into…

… blinqmedia-ads_api

Conflicts:
	lib/fb_graph.rb
  • Loading branch information...
2 parents 1efbfbb + 1296d2b commit 106165c376e7c9e5730241274da93c8049820a79 @nov nov committed Sep 3, 2011
View
12 README.rdoc
@@ -203,6 +203,10 @@ Both Facebook JavaScript SDK and normal OAuth2 flow is supported.
Below I show simple sample code.
You can also see https://github.com/nov/fb_graph_sample for more details Rails3 sample application.
+In addition, if you are migrating an application that uses old-style session keys you can exchange
+the keys for access tokens.
+See more here: http://developers.facebook.com/docs/authentication/fb_sig/
+
==== JavaScript SDK
fb_auth = FbGraph::Auth.new(YOUR_APP_ID, YOUR_APPLICATION_SECRET)
@@ -230,6 +234,14 @@ You can also see https://github.com/nov/fb_graph_sample for more details Rails3
access_token = client.access_token! # => Rack::OAuth2::AccessToken
FbGraph::User.me(access_token).fetch # => FbGraph::User
+==== Session Key Exchange
+
+ # setup client
+ fb_auth = FbGraph::Auth.new(YOUR_APP_ID, YOUR_APPLICATION_SECRET)
+ fb_auth.from_session_key('my-old-session-key')
+ fb_auth.access_token # => Rack::OAuth2::AccessToken
+
+
=== Analytics
app = FbGraph::Application.new(YOUR_APP_ID, :secret => YOUR_APPLICATION_SECRET)
View
2 lib/fb_graph/ad_account.rb
@@ -1,5 +1,7 @@
module FbGraph
class AdAccount < Node
+ include Connections::AdCampaigns
+
attr_accessor :account_id, :name, :account_status, :daily_spend_limit, :users, :currency, :timezone_id, :timezone_name, :capabilities, :account_groups
def initialize(identifier, attributes = {})
View
6 lib/fb_graph/ad_campaign.rb
@@ -10,7 +10,11 @@ def initialize(identifier, attributes = {})
end
%w(start_time end_time).each do |field|
- send("#{field}=", Time.parse(attributes[field.to_sym]).utc) if attributes[field.to_sym]
+ if val = attributes[field.to_sym]
+ # Handles integer timestamps and ISO8601 strings
+ time = Time.parse(val) rescue Time.at(val.to_i)
+ send("#{field}=", time)
+ end
end
end
end
View
17 lib/fb_graph/auth.rb
@@ -50,6 +50,19 @@ def from_signed_request(signed_request)
self
end
+ def from_session_key(session_key)
+ response = HTTPClient.new.post "#{ROOT_URL}/oauth/exchange_sessions", {:client_id => @client.identifier, :client_secret => @client.secret, :sessions => session_key}
+ if response.body && self.data = JSON.parse(response.body)
+ if self.data[0]
+ self.access_token = build_access_token(self.data[0].with_indifferent_access)
+ else
+ # If the session key is unknown or there's an error, Facebook returns null
+ self.access_token = nil
+ end
+ end
+ self
+ end
+
private
def get_access_token!(code)
@@ -62,7 +75,7 @@ def get_access_token!(code)
end
def build_access_token(data)
- expires_in = unless data[:expires].zero?
+ expires_in = unless data[:expires].nil? || data[:expires].zero?
data[:expires] - Time.now.to_i
end
Rack::OAuth2::AccessToken::Legacy.new(
@@ -74,4 +87,4 @@ def build_access_token(data)
end
require 'fb_graph/auth/cookie'
-require 'fb_graph/auth/signed_request'
+require 'fb_graph/auth/signed_request'
View
21 lib/fb_graph/connections/ad_campaign.rb
@@ -0,0 +1,21 @@
+module FbGraph
+ module Connections
+ module AdCampaigns
+ def ad_campaigns(options = {})
+ ad_campaigns = self.connection(:adcampaigns, options)
+ ad_campaigns.map! do |ad_campaign|
+ AdCampaign.new(ad_campaign[:id], ad_campaign.merge(
+ :access_token => options[:access_token] || self.access_token
+ ))
+ end
+ end
+
+ def ad_campaign!(options = {})
+ ad_campaign = post(options.merge(:connection => :adcampaigns))
+ AdCampaign.new(ad_campaign[:id], options.merge(ad_campaign).merge(
+ :access_token => options[:access_token] || self.access_token
+ ))
+ end
+ end
+ end
+end
View
17 spec/fb_graph/ad_campaign_spec.rb
@@ -23,6 +23,23 @@
ad_campaign.campaign_status.should == 1
ad_campaign.lifetime_budget.should == 100000
end
+
+ it 'should handle integer, string, or iso8601 timestamps' do
+ t = Time.parse("2011-09-01T00:00:00Z")
+ attributes = {
+ :id => '6003266501234',
+ :campaign_id => 6003266501234,
+ :account_id => 12345566,
+ :name => "Test Ad Campaign",
+ :end_time => "2011-09-10T00:00:00Z",
+ :daily_budget => 20000,
+ :campaign_status => 1,
+ :lifetime_budget => 100000
+ }
+ FbGraph::AdCampaign.new(attributes[:id], attributes.merge(:start_time => t.to_i)).start_time.should == t
+ FbGraph::AdCampaign.new(attributes[:id], attributes.merge(:start_time => t.to_i.to_s)).start_time.should == t
+ FbGraph::AdCampaign.new(attributes[:id], attributes.merge(:start_time => t.iso8601)).start_time.should == t
+ end
end
View
24 spec/fb_graph/auth_spec.rb
@@ -140,5 +140,29 @@
end
end
end
+
+ describe "#from_session_key" do
+ let(:session_key) { 'my_session_key'}
+
+ it "should exchange the session key for an oauth token" do
+ mock_graph :post, '/oauth/exchange_sessions', 'exchange_sessions_response' do
+ auth.access_token.should be_nil
+
+ auth.from_session_key(session_key)
+ auth.access_token.should be_a Rack::OAuth2::AccessToken::Legacy
+ auth.access_token.access_token.should == "my_oauth_token"
+ end
+ end
+
+
+ it "should handle null responses" do
+ mock_graph :post, '/oauth/exchange_sessions', 'exchange_sessions_null_response' do
+ auth.access_token.should be_nil
+
+ auth.from_session_key(session_key)
+ auth.access_token.should be_nil
+ end
+ end
+ end
end
View
1 spec/mock_json/exchange_sessions_null_response.json
@@ -0,0 +1 @@
+[null]
View
5 spec/mock_json/exchange_sessions_response.json
@@ -0,0 +1,5 @@
+[
+ {
+ "access_token": "my_oauth_token"
+ }
+]

0 comments on commit 106165c

Please sign in to comment.
Something went wrong with that request. Please try again.