Skip to content

Instantly share code, notes, and snippets.

@wildfalcon
Created December 22, 2010 14:09
Show Gist options
  • Save wildfalcon/751553 to your computer and use it in GitHub Desktop.
Save wildfalcon/751553 to your computer and use it in GitHub Desktop.
Read the fb cookie, and load the user based on the information there
require 'net/https'
class User < ActiveRecord::Base
after_create :pull_profile_from_facebook
def profile_url(size = "square")
"https://graph.facebook.com/#{uid}/picture?type=square"
end
private
def pull_profile_from_facebook
uri = URI.parse(URI.escape("https://graph.facebook.com/#{uid}?access_token=#{access_token}"))
puts uri.request_uri
http_session = Net::HTTP.new(uri.host, uri.port)
http_session.use_ssl = true
res = http_session.start { |http|
http.get(uri.request_uri)
}
self.attributes = JSON.parse(res.body).slice("name", "email")
self.save!
end
end
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<%= ENV["FB_APP_ID"] %>', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.sessionChange', function() {window.location.assign("/");});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment