Created
September 7, 2011 23:50
-
-
Save kkosuge/1202203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'facebook_oauth' | |
configure do | |
set :sessions, true | |
end | |
before do | |
next if request.path_info =~ /ping$/ | |
@user = session[:user] | |
@client = FacebookOAuth::Client.new( | |
:application_id => , | |
:application_secret => "", | |
:callback => "http://facebook.oauth.kksg.net/callback", | |
:token => session[:access_token] | |
) | |
end | |
get '/' do | |
erb :index | |
end | |
post '/' do | |
@client.me.feed(:create, :message => params[:create]) if params[:create] | |
erb :index | |
end | |
get '/auth' do | |
redirect @client.authorize_url | |
end | |
get '/callback' do | |
access_token = @client.authorize(:code => params[:code]) | |
session[:access_token] = access_token.token | |
session[:user] = @client.me.info | |
redirect '/' | |
end | |
get '/logout' do | |
session.delete :user | |
redirect '/' | |
end | |
__END__ | |
# | |
# layout.erb | |
# | |
<html> | |
<head> | |
<title>Facebook OAuth Test</title> | |
<link href="/main.css" media="screen" rel="Stylesheet" type="text/css" /> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | |
</head> | |
<body> | |
<h2>Facebook OAuth Test</h2> | |
<%= yield %> | |
<script src="https://gist.github.com/1202203.js?file=gistfile1.rb"></script> | |
</body> | |
</html> | |
# | |
# index.erb | |
# | |
<% if @user %> | |
<style type="text/css"> | |
#me li{display: inline;} | |
</style> | |
<ul id="me"> | |
<li><img src="http://graph.facebook.com/<%=@user['id']%>/picture"%></li> | |
<li><%= @user['name'] %> <a href="/logout">logout</a></li> | |
</ul> | |
<form method="post" action="/"> | |
<textarea name="create" rows="4" cols="50"> Post to your wall </textarea> | |
<input type="submit" value="post" /> | |
</form> | |
<p>token: <%= session[:access_token] %></p> | |
<% else %> | |
<div style="margin:20px;"><a href="/auth"><img src="facebook-login-button.png" /></a></div> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment