Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions lib/mini_fb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def profile_pic_album_id
def merge_aid(aid, uid)
uid = uid.to_i
ret = (uid << 32) + (aid & 0xFFFFFFFF)
# puts 'merge_aid=' + ret.inspect
return ret
end
end
Expand Down Expand Up @@ -182,8 +181,6 @@ def get(params)
# to hide value from simple introspection.
def MiniFB.call(api_key, secret, method, kwargs)

puts 'kwargs=' + kwargs.inspect if @@logging

if secret.is_a? String
secret = FaceBookSecret.new(secret)
end
Expand Down Expand Up @@ -226,7 +223,6 @@ def MiniFB.call(api_key, secret, method, kwargs)

body = response.body

puts 'response=' + body.inspect if @@logging
begin
data = JSON.parse(body)
if data.include?("error_msg")
Expand Down Expand Up @@ -512,7 +508,6 @@ def self.oauth_access_token(app_id, redirect_uri, secret, code)
oauth_url << "&client_secret=#{secret}"
oauth_url << "&code=#{CGI.escape(code)}"
resp = RestClient.get oauth_url
puts 'resp=' + resp.body.to_s if @@logging
params = {}
params_array = resp.split("&")
params_array.each do |p|
Expand All @@ -522,6 +517,58 @@ def self.oauth_access_token(app_id, redirect_uri, secret, code)
return params
end

# Returns a string containing your app access token
def self.app_access_token(app_id, secret)
oauth_url = "#{graph_base}oauth/access_token"
oauth_url << "?client_id=#{app_id}"
oauth_url << "&client_secret=#{secret}"
oauth_url << "&grant_type=client_credentials"
resp = RestClient.get oauth_url
return resp.body.split('=')[1]
end

# Returns a hash containing test user id, access_token, and login URL
# 'installed' is a boolean indicating whether the user has the app installed or not
# 'permissions' is a string containing comma-separated FB permissions that the test user granted to your app
def self.create_test_user(app_id, secret, installed, permissions)
url = "#{graph_base}#{app_id}/accounts/test-users"
params = {}
params['access_token'] = app_access_token(app_id, secret)
params['installed'] = installed
params['permissions'] = permissions
options = {}
options[:params] = params
options[:method] = :post
return fetch(url, options)
end

# Sends a friend request from test_user_1 to test_user_2 or accepts friendship if a request from test_user_2 is pending
# Raises an error if a request has already been sent or if users are already friends, returns response = true if request is successful
def self.send_test_user_friend_request(test_user_1, test_user_2)
raise "Invalid user hash" unless (%w{id access_token} - test_user_1.keys).empty? and (%w{id access_token} - test_user_2.keys).empty?
url = "#{graph_base}#{test_user_1['id']}/friends/#{test_user_2['id']}"
params = {'access_token' => test_user_1['access_token']}
options = {}
options[:params] = params
options[:method] = :post
return fetch(url, options)
end

def self.list_test_user(app_id, secret)
url = "#{graph_base}#{app_id}/accounts/test-users"
params = {}
params['access_token'] = app_access_token(app_id, secret)
options = {}
options[:params] = params
return fetch(url, options)
end

# Makes test_user_1 and test_user_2 friends by sending a request from one to the other then accepting it
# Raises an error if anything goes wrong, returns response = true if operation is successful
def self.add_test_user_friendship(test_user_1, test_user_2)
send_test_user_friend_request(test_user_2, test_user_1) if send_test_user_friend_request(test_user_1, test_user_2)
end

# Return a JSON object of working Oauth tokens from working session keys, returned in order given
def self.oauth_exchange_session(app_id, secret, session_keys)
url = "#{graph_base}oauth/exchange_sessions"
Expand All @@ -548,7 +595,6 @@ def self.authenticate_as_app(app_id, secret)
options[:method] = :get
options[:response_type] = :params
resp = fetch(url, options)
puts 'resp=' + resp.body.to_s if @@logging
resp
end

Expand All @@ -565,7 +611,6 @@ def self.search(access_token, q, options={})
params["type"] = options[:type] if options[:type]
params["fields"] = options[:fields].join(",") if options[:fields]
options[:params] = params
puts "#{url}\n"
return fetch(url, options)
end

Expand Down Expand Up @@ -697,8 +742,6 @@ def self.fetch(url, options={})

return res_hash
rescue RestClient::Exception => ex
puts "ex.http_code=" + ex.http_code.to_s
puts 'ex.http_body=' + ex.http_body if @@logging
res_hash = JSON.parse(ex.http_body) # probably should ensure it has a good response
raise MiniFB::FaceBookError.new(ex.http_code, "#{res_hash["error"]["type"]}: #{res_hash["error"]["message"]}")
end
Expand Down