Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created July 8, 2009 00:39
Show Gist options
  • Save jubishop/142490 to your computer and use it in GitHub Desktop.
Save jubishop/142490 to your computer and use it in GitHub Desktop.
http://www.jubishop.devrs006.facebook.com/login.php?api_key=API_KEY_HERE&next=http://www.jubishop.devrs006.facebook.com/connect/login_success.html&return_session=true&req_perms=offline_access,publish_stream,read_stream,email,create_event,rsvp_event,sms,status_update,photo_upload,video_upload,create_note,share_item
require 'rubygems'
require 'digest/md5'
require 'json'
require 'net/http'
class Facebook
public
# This is it.
def self.callMethod(methodName, args={})
args ||= {}
args.each_pair {|key, value| args[key] = jsonify(value) }
args['v'] = '1.0'
args['format'] = 'JSON'
args['method'] = methodName
args['api_key'] = @@apiKey
args['session_key'] = @@sessionKey
args['call_id'] = (@@call_id += 1)
args['ss'] = true
hashString = args.keys.sort.map{|key|
"#{key}=#{args[key]}"}.push(@@sessionSecret).join
args['sig'] = Digest::MD5.hexdigest(hashString)
res = Net::HTTP.post_form(
URI.parse('http://api.facebook.com/restserver.php'),
args)
JSON.parse(res.body.to_s)
end
@@apiKey = '6441b756e04e4b556084f1ebae7957d2'
@@sessionKey = '2.Kq_eNpy1bknRvWDp26FNbw__.86400.1247101200-688626964'
@@sessionSecret = '2zEok7ee_84opK2_ihuFSw__'
@@call_id = Time.now.tv_sec
private
def self.jsonify(item)
if (item.class == Hash)
item.each_pair {|key, value|
item[key] = jsonify(value)
}
item.to_json
elsif (item.class == Array)
item.map{|entry| jsonify(entry)}.to_json
else
item
end
end
end
# Some examples...
# Get all my friend ids
friend_ids = Facebook::callMethod("friends.get")
# Get some info about my friends, and print it out
friend_data = Facebook::callMethod("fql.query", {
'query' => "select name, status from user " +
"where uid in (#{friend_ids.join(',')})"
})
friend_data.each {|friend|
puts "Name: #{friend['name']}"
# Avoid NIL error when there's no status ever set, ever.
if (friend['status'])
puts "Status: #{friend['status']['message']}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment